Sei sulla pagina 1di 22

Question Paper

Object Oriented Programming and Java (MC221) : April 2006


Section A : Basic Concepts (30 Marks)

1.

This section consists of questions with serial number 1 - 30.

Answer all questions.


Each question carries one mark.

Maximum time for answering Section A is 30 Minutes.

(b) 1

(c) Garbage value

(e) Compile-time Error.


< Answer >

< Answer >

A public member of a class can be accessed in all the packages.


A private member of a class cannot be accessed by the methods of the same class.
A private member of a class cannot be accessed from its derived class.
A protected member of a class can be accessed from its derived class.
None of the above.
< Answer >

To prevent any method from overriding, we declare the method as,


(b) const

(c) final

(d) abstract

(e) none of the above.


< Answer >

Which one of the following is not true?


(a)
(b)
(c)
(d)
(e)

7.

(d) I = 3

one class inheriting from more super classes


more classes inheriting from one super class
more classes inheriting from more super classes
None of the above
(a) and (b) above.

(a) static
6.

< Answer >

Which statement is not true in java language?


(a)
(b)
(c)
(d)
(e)

5.

(e) -1.

Multiple inheritance means,


(a)
(b)
(c)
(d)
(e)

4.

(d) Null

What will be printed as the output of the following program?


public class testincr
{
public static void main(String args[])
{
int i = 0;
i = i++ + i;
System.out.println("I = " +i);
}
}
(a) I = 0
(b) I = 1
(c) I = 2

3.

< Answer >

The default value of a static integer variable of a class in Java is,


(a) 0

2.

A class containing abstract methods is called an abstract class.


Abstract methods should be implemented in the derived class.
An abstract class cannot have non-abstract methods.
A class must be qualified as abstract class, if it contains one abstract method.
None of the above.
< Answer >

The fields in an interface are implicitly specified as,


(a) static only
(d) both static and final

(b) protected
(e) none of the above.

(c) private

8.

< Answer >

What is the output of the following program:


public class testmeth
{
static int i = 1;
public static void main(String args[])
{
System.out.println(i+ , );
m(i);
System.out.println(i);
}
public void m(int i)
{
i += 2;
}
}
(a) 1 , 3

9.

(b) 3 , 1

(c) 1 , 1

(d) 1 , 0

(e) none of the above.


< Answer >

Which of the following is not true?


(a)
(b)
(c)
(d)
(e)

An interface can extend another interface.


A class which is implementing an interface must implement all the methods of the interface.
An interface can implement another interface.
An interface is a solution for multiple inheritance in java.
None of the above.
< Answer >

10. Which of the following is true?


(a)
(b)
(c)
(d)
(e)
11.

A finally block is executed before the catch block but after the try block.
A finally block is executed, only after the catch block is executed.
A finally block is executed whether an exception is thrown or not.
A finally block is executed, only if an exception occurs.
None of the above.
< Answer >

Among these expressions, which is(are) of type String?


(a) "0"
(d) Both (A) and (B) above

(b) "ab" + "cd"


(e) (A), (B) and (C) above.

12. Consider the following code fragment

(c) '0'
< Answer >

Rectangle r1 = new Rectangle();


r1.setColor(Color.blue);
Rectangle r2 = r1;
r2.setColor(Color.red);
After the above piece of code is executed, what are the colors of r1 and
r2 (in this order)?
(a) Color.blue
Color.red
(b) Color.blue
Color.blue
(c) Color.red
Color.red
(d) Color.red
Color.blue
(e) None of the above.
13. What is the type and value of the following expression? (Notice the integer division)

< Answer >

-4 + 1/2 + 2*-3 + 5.0


(a) int -5
(d) double -5.0

(b) double -4.5


(e) None of the above.

(c) int -4
< Answer >

14. What is printed by the following statement?


System.out.print("Hello,\nworld!");
(a) Hello, \nworld! (b) Hello, world!
(d) "Hello, \nworld!" (e) None of the above.

Hello,
(c) world!
< Answer >

15. Consider the two methods (within the same class)


public static int foo(int a, String s)
{
s = "Yellow";
a=a+2;
return a;
}
public static void bar()
{
int a=3;
String s = "Blue";
a = foo(a,s);
System.out.println("a="+a+" s="+s);
}
public static void main(String args[])
{
bar();
}
What is printed on execution of these methods?
(a) a = 3 s = Blue
(d) a = 5 s = Blue

(b) a = 5 s = Yellow
(e) none of the above.

(c) a = 3 s = Yellow
< Answer >

16. Which of the following variable declaration would NOT compile in a java program?
(a) int var;

(b) int VAR;

(c) int var1;

(d) int var_1;

(e) int 1_var;.


< Answer >

17. Consider the following class definition:


public class MyClass
{
private int value;
public void setValue(int i){ /* code */ }
// Other methods...
}
The method setValue assigns the value of i to the instance field value. What could you write for the
implementation of setValue?
(a) value = i;
(d) Both (A) and (B) and above

(b) this.value = i;
(e) (A), (B) and (C) above.

(c) value == i;

18. Which of the following is TRUE?


(a)
(b)
(c)
(d)
(e)

In java, an instance field declared public generates a compilation error.


int is the name of a class available in the package java.lang
Instance variable names may only contain letters and digits.
A class has always a constructor (possibly automatically supplied by the java compiler).
The more comments in a program, the faster the program runs.

< Answer >

< Answer >

19. A constructor
(a)
(b)
(c)
(d)
(e)

Must have the same name as the class it is declared within.


Is used to create objects.
May be declared private
Both (A) and (B) above
(a), (b) and (c) above.
< Answer >

20. Consider,
public class MyClass
{
public MyClass(){/*code*/}
// more code...
}
To instantiate MyClass, you would write?
(a)
(b)
(c)
(d)
(e)

MyClass mc = new MyClass();


MyClass mc = MyClass();
MyClass mc = MyClass;
MyClass mc = new MyClass;
The constructor of MyClass should be defined as, public void MyClass(){/*code*/}.
< Answer >

21. What is byte code in the context of Java?


(a)
(b)
(c)
(d)
(e)

The type of code generated by a Java compiler.


The type of code generated by a Java Virtual Machine.
It is another name for a Java source file.
It is the code written within the instance methods of a class.
It is another name for comments written within a program.
< Answer >

22. What is garbage collection in the context of Java?


(a) The operating system periodically deletes all the java files available on the system.
(b) Any package imported in a program and not used is automatically deleted.
(c) When all references to an object are gone, the memory used by the object is automatically
reclaimed.
(d) The JVM checks the output of any Java program and deletes anything that doesn't make sense.
(e) Janitors working for Sun Micro Systems are required to throw away any Microsoft documentation
found in the employees' offices.

< Answer >

23. You read the following statement in a Java program that compiles and executes.
submarine.dive(depth);
What can you say for sure?
(a)
(b)
(c)
(d)
(e)

depth must be an int


dive must be a method.
dive must be the name of an instance field.
submarine must be the name of a class
submarine must be a method.
< Answer >

24. The java run time system automatically calls this method while garbage collection.
(a) finalizer()
(d) finalized()

(b) finalize()
(e) none of the above.

25. The correct order of the declarations in a Java program is,


(a) Package declaration, import statement, class declaration
(b) Import statement, package declaration, class declaration

(c) finally()
< Answer >

(c) Import statement, class declaration, package declaration


(d) Class declaration, import statement, package declaration
(e) Class declaration, package declaration, import statement.
< Answer >

26. An overloaded method consists of,


(a)
(b)
(c)
(d)
(e)

The same method name with different types of parameters


The same method name with different number of parameters
The same method name and same number and type of parameters with different return type
Both (a) and (b) above
(a), (b) and (c) above.
< Answer >

27. A protected member can be accessed in,


(a) a subclass of the same package
(c) a non-subclass of different package
(e) the same class.

(b) a non-subclass of the same package


(d) a subclass of different package

Which is the false option?


< Answer >

28. What is the output of the following code:

(a) true

class eq
{
public static void main(String args[])
{
String s1 = Hello;
String s2 = new String(s1);
System.out.println(s1==s2);
}
}
(b) false
(c) 0

(d) 1

(e) Hello.
< Answer >

29. All exception types are subclasses of the built-in class


(a) Exception
(d) Throwable

(b) RuntimeException
(e) None of the above.

(c) Error

30. When an overridden method is called from within a subclass, it will always refer to the version of that
method defined by the
(a)
(b)
(c)
(d)
(e)

Super class
Subclass
Compiler will choose randomly
Interpreter will choose randomly
None of the abvove.
END OF SECTION A

< Answer >

Section B : Problems (50 Marks)

1.

This section consists of questions with serial number 1 5.


Answer all questions.

Marks are indicated against each question.


Detailed workings should form part of your answer.

Do not spend more than 110 - 120 minutes on Section B.

Write a Java program to implement a bank account having the methods deposit(), withdraw(), and showbalance()
with appropriate parameters.
(10 marks) < Answer >

2.

Write a Java program to implement the method overloading concept for a problem, where some students submit
directly character grades and some students submit marks for which we have to evaluate grades. The method
accepts a students marks or grades and finally stored as character grades.
(10 marks) < Answer >

3.

Create a super class Shapes. Create subclasses for different shapes Rectangle, Square, Circle, and for each
of them calculate area and display values with common methods.
(10 marks) < Answer >

4.

Write a Java program to demonstrate at least five mouse event handlers.


(10 marks) < Answer >

5.

Write a Java program to demonstrate key event handlers.


(10 marks) < Answer >

END OF SECTION B

Section C : Applied Theory (20 Marks)

6.

This section consists of questions with serial number 6 - 7.

Answer all questions.


Marks are indicated against each question.

Do not spend more than 25 -30 minutes on section C.

What is multithreading? Explain the two ways to implement multithreading in Java. Also explain the thread states.
(10 marks) < Answer >

7.

What is meant by inheritance? Explain the types of inheritance. What is an Interface? Explain it.
(10 marks) < Answer >

END OF SECTION C
END OF QUESTION PAPER

Suggested Answers
Object Oriented Programming and Java (MC221) : April 2006
Section A : Basic Concepts
1.

Answer :
(a)
Reason: The default value of a static integer
variable of a class in Java is 0.

< TOP
>

2.

Answer :
(b)
Reason: 1
The execution goes on like this:
int i = 0; // i becomes 0
i = 0 + i; // now, i becomes 1
i = 0 + 1; // perform addition and
assign 1 to i.

< TOP
>

3.

Answer :

< TOP
>

Reason:

(a)

Multiple inheritance means one class inheriting


from more super classes.

4.

Answer :
(b)
Reason: Private members of a class can be accessed
by the methods within the same class.

< TOP
>

5.

Answer :
(c)
Reason: Final methods of the base class cannot be
overridden in the derived Class.

< TOP
>

6.

Answer :
(c)
Reason: An abstract class can contain both abstract
and non-abstract methods.

< TOP
>

7.

Answer :

< TOP
>

Reason:

(d)

The fields in an interface are implicitly specified as


both static and final.

8.

Answer :
(c)
Reason: Parameter values are passed by value in the
calling of a method, and so a copy of the
value is created in the method, and the
original value is not affected by the method
call.

< TOP
>

9.

Answer :
(c)
Reason: An interface can extend another interface
but not implement.

< TOP
>

10. Answer :

(c)

< TOP
>

Reason:

A finally block is executed whether an exception is


thrown or not is correct.

11. Answer :
Reason:

(d)

Strings are "0" and "ab" + "cd" .

< TOP
>

12. Answer :
(c)
Reason: Both r1 and r2 are referring the same object
of Rectangle class. So, finally the Color of
the object is changed to red.

< TOP
>

13. Answer :
(d)
Reason: The execution goes on like this:
-4 + 1/2 + 2*-3 + 5.0;
-4 + 0 + -6 + 5.0; // integer division: 1/2
truncates .5
-10 + 5.0; // higher type is double 5.0, so
-10 is casted to double
-5.0; // finally, double -5.0.

< TOP
>

14. Answer :

< TOP
>

Reason:

(c)

The statement
System.out.print("Hello,\nworld!");
gives output as
Hello,
world!

15. Answer :
(d)
Reason: a value is returned from the method and
so it is 5. But the string will remain same,
as it is passed by value to the method.

< TOP
>

16. Answer :
(e)
Reason: The first character of a variable name
should not be a digit.

< TOP
>

17. Answer :
(d)
Reason: == is a comparison operator.

< TOP
>

18. Answer :
(d)
Reason: A class will always have a constructor,
either provided by the user or a default
constructor provided by the compiler.

< TOP
>

19. Answer :

< TOP
>

Reason:

(e)

A constructor

Must have the same name as the class it is


declared within.

Is used to create objects.

May be declared private.

20. Answer :
(a)
Reason: An object is created by using a new
operator.

< TOP
>

21. Answer :
(a)
Reason: Java compiler compiles the source code file
and converts it into a class file, which
consists of byte code.

< TOP
>

22. Answer :

< TOP
>

Reason:

(c)

Garbage collection in the context of Java is when all


references to an object are gone, the memory used
by the object is automatically reclaimed.

23. Answer :
(b)
Reason: The other choices can be allowed, but not
must

< TOP
>

24. Answer :
(b)
Reason: finalize() method is automatically called
by the java compiler before destroying the
object to free any resources.

< TOP
>

25. Answer :
(a)
Reason: First the package name is defined. Then the
import statements if any. And then the class
declaration goes on.

< TOP
>

26. Answer :
(d)
Reason: Even though the return type varies, it will
not be considered for overloading concept.
Only method name and parameters are
considered.

< TOP
>

27. Answer :
(c)
Reason: A protected member cannot be accessed in
non-subclasses of different packages.

< TOP
>

28. Answer :
(b)
Reason: Since , the contents of the two String
objects are identical, but they are distinct
objects.

< TOP
>

29. Answer :
(d)
Reason: Throwable is the super class of all the
exception classes.

< TOP
>

30. Answer :
(b)
Reason: The compiler will always selects the
version of the method in subclass.

< TOP
>

superclass method can be called by


explicitly assigning super reference.

Section B : Problems
1.

class Bank
{
float bal;
public Bank()
{
bal=0.0;
}
public void deposit(float money)
{
bal = bal + money;
}
public void withdraw(float money)
{
if (money<=bal)
bal = bal money;
else
System.out.println(Not possible to withdraw);
}
public void display()
{
System.out.println(Account Balance is, +bal);
}
}
public class TestBank
{
public static void main(String args[])
{
Bank b = new Bank();
b.deposit(100);
b.display();
b.withdraw(50);
b.display();
}
}
< TOP >

2.

class StudentGrades

{
char grade;
public void evalgrad(int marks)
{
if (marks > 80) grade = A;
else if (marks > 60) grade = B;
else if (marks > 40) grade = C;
else grade = F;
System.out.println(grade);
}
public void evalgrad(char grad)
{
grade = grad;
System.out.println(grade);
}
}
public class Testgrade
{
public static void main(String args[])
{
StudentGrades s = new StudentGrades();
s.evalgrad(C);
s.evalgrad(57);
}
}
< TOP >

3.

public class Shapes


{
public double area;
public void display()
{
System.out.println(Display from shapes : Area = +area);
}
public void calculate()
{

System.out.println(calculating from shapes: Area);


}
}
public class Circle() extends Shapes
{
float radius;
public void display()
{
System.out.println(area);
}
public void calculate()
{
area = 22/7*radius*radius;
}
}
public class Rectangle() extends Shapes
{
float length, breadth;
public void display()
{
System.out.println(area);
}
public void calculate()
{
area = length*breadth;
}
}
public class Square() extends Shapes
{
float side;
public void display()
{
System.out.println(area);
}
public void calculate()
{
area = side*side;
}

}
< TOP >

4.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MouseEvents extends Applet implements MouseListener, MouseMotionListener
{
String msg = ;
int mouseX = 0, mousey = 0;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
mouseX = 0;
mouseY = 10;
msg = Mouse Clicked.;
repaint();
}
public void mouseEntered(MouseEvent me)
{
mouseX = 0;
mouseY = 10;
msg = Mouse Entered;
repaint();
}
public void mouseExited(MouseEvent me)
{
mouseX = 0;
mouseY = 10;
msg = Mouse exited;
repaint();
}
public void mousePressed(MouseEvent me)

{
mouseX = me.getX();
mouseY = me.getY();
msg = down;
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg, mouseX, mouseY);
}
}
< TOP >

5.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class SimpleKey extends Applet implements KeyListener
{
String msg = ;
int X = 10, Y = 20;
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent ke)
{
showStatus(keydown);
}
public void keyReleased(KeyEvent ke)
{
showStatus(key up);
}
public void keyTyped(KeyEvent ke)
{
msg += ke.getKeyChar();
repaint();
}

public void paint(Graphics g)


{
g.drawString(msg, X, Y);
}
}
< TOP >

Section C: Applied Theory


6.

Multithreading:A Multithreaded program contains two or more parts that can run concurrently. Each part of
such a program is called a thread, and each thread defines a separate path of execution. It is
similar to thread-based multitasking. The thread is the smallest unit of dispatchable code. This
means that a single program can perform two or more tasks simultaneously. For instance, a text
editor can format text at the same time that it is printing, as long as these two actions are being
performed by two separate threads.
Multitasking threads require less overhead than multitasking processes. Processes are
heavyweight tasks that require their own separate address spaces. Interprocess communication
is expensive and limited. Context switching from one process to another is also costly. Threads,
on the other hand, are lightweight. They share the same address space and cooperatively share
the same heavyweight process. Context switching from one thread to the next is low cost.
While java programs make use of process-based multitasking environments, process-based
multitasking is not under the control of java.
Thread States:Threads exist in several states. A thread can be running. It can be ready to run as soon as it gets
CPU time. A running thread can be suspended, which temporarily suspends its activity. A
suspended thread can then be resumed, allowing it to pick up where it left off. A thread can be
blocked when waiting for a resource. At any time, a thread can be terminated, which halts its
execution immediately. Once terminated, a thread cannot be resumed.
Two ways to implement multithreading:The Thread Class
To create a new thread, create a new class that extends Thread, and then to create an instance of
that class. The extending class must override the run() method, which is the entry point for the
new thread. it must also call start() to begin execution of the new thread.
The Thread class defines several methods that help manage threads.
Method
Meaning
getName
Obtain a threads name
getPriority
Obtain a threads priority
isAlive
Determine if a thread is still running.
join
Wait for a thread to terminate
run
Entry point for the thread

sleep
Suspend a thread for a period of time
start
Start a thread by calling its run method
Here is an example program extending Thread:
class Newthread extends Thread
{
Newthread()
{
super(demo);
System.out.println(child thread : + this);
start();
}
public void run()
{
try
{
for (int i=5; i>0; i--)
{
System.out.println(child thread : +i);
Thread.sleep(500);
}
}
catch(InterrutedException e)
{
System.out.println(child interrupted);
}
System.out.println(exits child);
}
}
Implementing Runnable Interface
The easiest way to create a thread is to create a class that implements the Runnable interface.
Runnable abstracts a unit of executable code. You can construct a thread on any object that
implements Runnable. To implement Runnable, a class need only implement a single method
called run(), which is declared like this:
public void run()
Inside run(), you will define the code that constitutes the new thread.
After you create a class that implements Runnable, you will instantiate an object of type Thread
from within that class.
After the new thread is created, it will not start running until you call its start() method, which
is declared within Thread. In essence, start() executes a call to run(). The start() method is

shown here:
void start()
Here is an example program that creates a new thread implementing Runnable interface:
class Newthread implements Runnable
{
Thread t;
NewThread()
{
t = new Thread(this, demo);
System.out.println(child thread : +t);
t.start();
}
public void run()
{
try
{
for (int i=5; i>0; i--)
{
System.out.println(child thread: +i);
Thread.sleep(500);
}
catch(InterruptedException e)
{
System.out.println(child interrupted);
}
System.out.println(exiting child thread);
}
}
< TOP >

7.

Inheritance:Inheritance is the process by which one object acquires the properties of another object. This
supports the concept of hierarchical classification. By use of inheritance, an object need only
define those qualities that make it unique within its class. It can inherit its general attributes
from its Parent.
Using inheritance, you can create a general class that defines traits common to a set of related
items. This class can then be inherited by other, more specific classes, each adding those things
that are unique to it. In the terminology of Java, a class that is inherited is called a super class.
The class that does the inheriting is called a sub class. Therefore, a sub class is a specialized
version of super class. It inherits all of the instance variables and methods defined by the super

class and add its own, unique elements.


Inheritance is achieved by using the keyword extends before the super class name, while
defining the sub class.
class <sub class name> extends <super class name> { }
Types of Inheritance:We have different types of inheritance:

(sub class)

Here is an example showing the inheritance concept:

class A
{
String s = hai;
void say()
{
System.out.println(s+ , i am fine in A);
}
}
class B extends A
{
String s = hello;
void tell()
{
System.out.println(s + , i am fine in B);
say();
}
}
Interface:An interface definition has two components: the interface declaration and the interface body.
The interface declaration defines various attributes of the interface, such as its name and
whether it extends other interfaces. The interface body contains the constants and the method
declarations for that interface.
The Interface Declaration
Two elements are required in an interface declaration the interface keyword and the name of
the interface. The public access specifier indicates that the interface can be used by any class in
any package. If you do not specify that the interface is public, your interface will be accessible
only to classes defined in the same package as the interface.
An interface declaration can have one other component: a list of superinterfaces. An interface
can extend other interfaces, just as a class can extend or subclass another class. However,
whereas a class can extend only one other class, an interface can extend any number of
interfaces. The list of superinterfaces is a comma-separated list of all the interfaces extended by
the new interface.
The Interface Body
The interface body contains method declarations for all the methods included in the interface. A
method declaration within an interface is followed by a semicolon ( ;) because an interface does
not provide implementations for the methods declared within it. All methods declared in an
interface are implicitly public and abstract.
An interface can contain constant declarations in addition to method declarations. All constant
values defined in an interface are implicitly public, static, and final.
Member declarations in an interface prohibit the use of some declarations; you cannot use

transient, volatile, or synchronized in a member declaration in an interface. Also, you cannot


use the private and protected specifiers when declaring members of an interface.
An interface defines a protocol of behavior. A class that implements an interface adheres to the
protocol defined by that interface. To declare a class that implements an interface, include an
implements clause in the class declaration. Your class can implement more than one interface
(the Java platform supports multiple inheritance for interfaces), so the implements keyword is
followed by a comma-separated list of the interfaces implemented by the class.
When a class implements an interface, it is essentially signing a contract. Either the class must
implement all the methods declared in the interface and its superinterfaces, or the class must be
declared abstract. The method signature the name and the number and type of arguments
in the class must match the method signature as it appears in the interface.
Here is an example showing the interface:
interface A
{
void play(int n); // automatically public
String give();
}
class B implements A
{
public void play(int n)
{
System.out.println(inside play : +n);
}
public String give()
{
return this is example;
}
}
< TOP >

< TOP OF THE DOCUMENT >

Potrebbero piacerti anche