Sei sulla pagina 1di 10

1) Given the following class,which statements can be inserted at position 1 without

causing a compilation error?


public class test1
{
int a;
int b=0;
static int c;
public void disp()
{
int d;
int e=0;
// Position 1
}
}
Select 4 correct answers
a) a++ b) b++ c) c++ d) d++ e) e++
2) What is wrong in the following
class MyException extends Exception{}
public class test2
{
public void disp()
{
try
{
first();
}
finally
{
second();
}
catch(MyException m)
{
}
}
public void first()throws MyException
{
throw new MyException();
}
public void second()throws RuntimeException
{
throw new RuntimeException();
}
}

Select the one correct answer


a) Since the method disp() does not catch the exception generated by the
method second(),it must declare the RuntimeException in a throws clause.
B) A try block cannot be followed by both a catch and a finally
block.
C) An empty catch block is not allowed.
d) A catch block cannot follow a finally
3) Given that a static method disp() in the class Shape.
Shape is an independent class which is not a sub class of any class or interface.
Which block of code will succeed in starting a new thread that will call the disp();
Select the one Correct answer.
a) Runnable rn=new Runnable() //Anonymous class
{
public void run()
{
Shape.disp();
}
};
Thread t=new Thread(rn);
t.start();
b) Thread t=new Thread()
{
public void start()
{
Shape.disp();
}
};
t.start();
c) Runnable rn=new Runnable()
{
public void run()
{
Shape.disp();
}
};
rn.start();

//Anonymous class

//Anonymous class

d) Thread t=new Thread(new Shape());


t.start();

e) Runnable rn=new Runnable() //Anonymous class


{
public void run()
{
Shape.disp();
}
};
rn.run();

4) What will be the result of attempting to compile and run the following code?
public class test3
{
static int a;
int b;
public test3()
{
int c;
c=a;
a++;
b+=c;
}
public static void main(String args[])
{
new test3();
}
}
Select the one correct answer
a) The code will fail to compile since the constructor is trying to access static
members
b) The code will fail to compile since the constructor is trying to use static field
a before it has been initialized.
c) The code will fail to compile since the constructor is trying to use static field
b before it has been initialized.
d) The code will fail to compile since the constructor is trying to use static field
c before it has been initialized.
e) The code will compile and run without any problems.
5) What will be the output ?

public class test4


{
public static void main(String args[])
{
System.out.println(9^2);
}
}
a) 81 b) 7
c) 11 d) 0 e) false
6) Which statement is true about the compilation and execution of the following program
with assertions enabled ?
public class test5
{
String s1;
String s2="hello";
String s3;
test5()
{
s1="hello";
s3="hello";
}
void disp()
{
String s4="hello";
String s5=new String("hello");
assert(s1.equals(s2)); // (1)
assert(s2.equals(s3)); // (2)
assert(s3==s4);
// (3)
assert(s4==s5);
// (4)
}
public static void main(String args[])
{
new test5().disp();
}
}
Select one correct answer
a) The compilation will fail
b) The assertion on line marked (1) will fail
c) The assertion on line marked (2) will fail
d) The assertion on line marked (3) will fail
e) The assertion on line marked (4) will fail
f) The program will run without any erros.

7) Which declarations of main() method are valid in order to start the execution of an
application?
Select two correct answers
a)
b)
c)
d)
e)

public void main(String args[])


public void static main(String args[])
public static main(String args[])
final public static void main(String [] args)
public static void main(String args[])
8) Under which circumstances will a thread stop ?
Select the one correct answer
a) The call to start() method of the Thread object returns.
b) The run() method that the thread is executing ends.
c) The suspend() method is called on the Thread object
d) The wait() method is called on the Thread object.
9) When creating a class that associates a set of keys with a set of
values,which of these interfaces is most applicable ?
Select the one correct answer
a) Collection
b) Set
c) SortedSet
d) Map
10) what will be the output ?
class base
{
int i;
base()
{
add(1);
}
void add(int v)
{
i+=v;
}
void print()
{
System.out.println(i);
}
}
class sub extends base

{
sub()
{
add(2);
}
void add(int v)
{
i+=v*2;
}
}
public class test6
{
static void disp(base b)
{
b.add(8);
b.print();
}
public static void main(String args[])
{
disp(new sub());
}
}
Select the one correct answer
a) 9 b) 18 c) 20 d) 21 e) 22
11) Which Collection implementation is suitable for maintaining an ordered
sequence of objects,when objects are frequently inserted and removed
from the middle of the sequence ?
Select the one correct answer
a) TreeMap
b) HashSet
c) Vector
d) LinkedList
e) ArrayList
12) Which statements can be inserted at the indicated position in the
following code to make the program print 1 on the standard output when
executed ?
public class test7
{
int a=1;
int b=1;

int c=1;
class inner
{
int get()
{
int c=3;
// Insert Here
return c;
}
}
test7()
{
inner i=new inner();
System.out.println(i.get());
}
public static void main(String args[])
{
new test7();
}
}
Select one correct answer
a) c=c;
b) c=b;
c) c=this.a;
d) c=this.b;
e) c=test7.this.a;
12) Which statement ,when inserted at the indicated position in the following
code,will cause a runtime exception ?
class A1{}
class B1 extends A1{}
class C1 extends A1{}
public class test8
{
public static void main(String args[])
{
A1 x=new A1();
B1 y=new B1();
C1 z=new C1();
//Insert Here
System.out.println("over");
}
}

Select one correct answer


a) x=y;
b) z=x;
c) y=(B1)x;
d) z=(C1)y;
e) y=(A1)y;
13) A monitor must extends either Thread class or implements Runnable
interface
a) false b) true
14) A method within a class is only accessible by classes that are defined
within the same package as the class of the method. How can such a
restrication be enforced ?
Select the one correct answer
a) Declare the method with the keyword public
b) Declare the method with the keyword protected
c) Do not declare the method with any modifiers.
d) Declare the method with the keyword private
e) Declare the method with the keyword package
15) which statements are true about the methods notify() and notifyAll() ?
Select the three correct answers.
a) An instance of the class Thread has a method named notify() that can be
invoked.
b) A call to the method notify() will wake the thread that currently owns the
lock the object.
c) The method notify() is synchronized
d) notify() or notifyAll() can be called only in synchronized block or method
e) The method notifyAll() is defined in class Thread
f) When there is more than one thread waiting to obtain the lock of an
object,there is no way to be sure which thread will be notified by the
notify() method.
16) what is the result of compiling and executing the following application
import java.awt.*;
public class test9 extends Frame
{
test9()
{
setSize(400,400);
Button b=new Button("Apply");

add(b);
}
public static void main(String args[])
{
test9 t=new test9();
t.setVisible(true);
}
}
a)
b)
c)
d)

There is a compilation error because constructor is not public


Exception because there is no layout specified
The program displays button occupying entire frame
The program displays empty frame

17) Select correct statements


a) Serializable interface extends Externalizable
b) Serializable interface is empty
c) Externalizable interface extends Serializable
d) transient members can be serialized
18)
class Foo
{
int num;
Bar comp=new Bar();
}
class Bar
{
boolean flag;
}
class Baz extends Foo
{
Bar thing=new Bar();
double d;
}
Select the three correct answers
a) A Bar is a Baz
b) A Foo has a Bar
c) A Baz is a Foo
d) A Foo is a Baz
e) A Baz has a Bar.
19) sleep() can release the lock on an object
a) true b) false

20) which statements are true about casting and conversion ?


Select the three correct answers
a)
b)
c)
d)
e)

Conversion from int to long does not need a cast


Conversion from byte to short does not need a cast
Conversion from float to long does not need a cast
Conversion from short to char does not need a cast
Conversion from boolean to int using a cast is not possible

Potrebbero piacerti anche