Core Java Theory Test
Core Java Theory Test
//Anonymous class
//Anonymous class
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 ?
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)
{
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");
}
}
add(b);
}
public static void main(String args[])
{
test9 t=new test9();
t.setVisible(true);
}
}
a)
b)
c)
d)