Questions
Questions
a. Compile error
b. Runtime Exception. ArrayOutOfBounds
c. Prints 4
d. Prints 5
e. Compiles with warning
-----------------------------------------------------------------------------------------
2.
a. Compile error
b. Runtime Exception. ArrayOutOfBounds
c. Prints 1 2
d. Prints null
e. Compiles with warnings
-----------------------------------------------------------------------------------------
3.
class test
{
{ System.out.println("one"); }
static { System.out.println("static one"); }
private test(){
{ System.out.println("two"); }}
{ System.out.println("three"); }
static
{ System.out.println("static two");
}
4.
a. Prints null
b. Runtime NullpointerExcepion
c. Compile time error.
-----------------------------------------------------------------------------------------
5.
class test
{
public static void main(String[] args)
{
test inst_test = new test();
inst_test.method ( 1 , 1 , 1);
inst_test.method( new Integer(1) , new Integer(2) , new Integer(3) );
inst_test.method ( 1 , new Integer(5) );
inst_test.method ( new Integer(10) , 1 );
}
a. Fly in the pie Eye in the sky Eye in the sky Eye in the sky
b. Eye in the sky Eye in the sky Eye in the sky Eye in the sky
c. Compile error
d. Runtime Exception
e. None of the above.
-----------------------------------------------------------------------------------------
6.
class test
{
public static void main(String[] args)
{
test inst_test = new test();
String pig[][] = { {"one little piggy"}, {"two little piggies"}, {"three little piggies"} };
for ( Object []oink : pig )
{
for ( Object piggy : oink )
{
System.out.println(piggy);
}
}
}
}
7.
class test
{
public static void main(String[] args)
{
test inst_test = new test();
int i1 = 2000;
int i2 = 2000;
int i3 = 2;
int i4 = 2;
Integer Ithree = new Integer(2); // 1
Integer Ifour = new Integer(2); // 2
System.out.println( Ithree == Ifour );
inst_test.method( i3 , i4 );
inst_test.method( i1 , i2 );
}
public void method( Integer i , Integer eye )
{
System.out.println(i == eye );
}
}
interface face
{
void smile();
}
class test implements face
{
public static void main(String[] args)
{
test evil_laugh = new test();
evil_laugh.smile();
}
void smile()
{
System.out.println("Muahahahahahhahaha");
}
}
a. Prints Muahahahahahhahaha
b. Compile error
c. Runtime Exception
d Prints nothing
e. None of the above.
-----------------------------------------------------------------------------------------
9.
abstract class ab
{ abstract private void smile();
}
class test extends ab
{ public static void main(String[] args)
{
test inst_test = new test();
}
}
10.
11.
Drag and Drop either “true” or “false” above the following options that are based on this code
fragment.
( Imagine that true and false are two fragments on the screen that can be dragged onto the
options shown )
Code Fragment : List < ? > l = new ArrayList< Integer > ();
Options:
12.
If two instances, test_one and test_two of Serializable class test are written to a file named
“serial.serial” in that order, which of the following are true ?
a. When reading back from the file, test_two is the first object that is read.
b. When reading back from the file, test_one is the first object that is read.
c. The object cannot be read from a file named “serial.serial”.
d. None of the above.
13.
class test
{
switch ( x )
{
case x4:
System.out.println("4");
break;
case x:
System.out.println("x");
break;
}
}
}
a. Compile error
b. Runtime error
c. Prints x
d. Prints 4
e. None of the above.
14.
class num
{
int x=9;
}
interface blob
{
final num n = new num();
final Integer number = 1;
}
a. Compile error at 1
b. Runtime error at 1
c. Compiler error at 2
d. Runtime error at 2
e. Compiles and runs fine
15.
enum cafe {
BIG ( 10 ) ,
SMALL ( 1 ),
MED ( 5 )
int mySize = 0;
cafe ( int size )
{
mySize = size;
a. Compiles fine
b. Compiler error
c. Runtime Exception occurs if mySize is accessed.
16.
class Animal
{
void method throws IOException
{}
}
17.
class Animal
{
void method () throws Exception
{}
Animal( String name )
{
System.out.println(name);
}
}
class test
{
a. Prints Giraffe
b. Compile error
c. Runtime error
d. Prints nothing
e. None of the above
18.
class test
{
int x;
public static void main ( String [] args )
{
final int i;
i = 127;
byte b = i;
System.out.println(b);
}
}
19.
class test
{
public static void main ( String [] args )
{
methodOne(20);
}
static void methodOne( long l )
{
System.out.println("long");
}
static void methodOne( float f )
{
System.out.println("float");
}
}
a. Prints long
b. Prints float
c. Compile error: Too ambiguous
d. Runtime error
e. None of the above
20.
import java.util.*;
class test
{
public static void main ( String [] args )
{
List < String > least = new ArrayList < String > ();
List list = new ArrayList();
meth(list);
seth(least);
}
public static void meth(List < String > list)
{
System.out.println("List");
}
public static void seth(List list)
{
System.out.println("Mist");
}
}
Which function call(s) is/are allowed? Here allowed refers to legality, and that compilations
succeeds.
21.
import java.util.*;
class test
{
public static void main ( String [] args )
{
List < Integer > list = new ArrayList < Integer > ();
for ( int i = 1 ; i<10 ; i++ )
{
list.add(i);
}
list.remove( new Integer(4) ); // 1
list.remove( 1 ); // 2
}
22.
import java.util.*;
class test
{
public static void main ( String [] args )
{
Map < Integer , String > map = new LinkedHashMap < Integer , String > ();
Map < Integer , String > sap = new HashMap < Integer , String > ();
populate( map );
populate( sap );
System.out.println( map.get(1) + sap.get(1) );
}
a. Prints 11
b. Prints 2
c. Compile error
d. Runtime error
e. nullnull
23.
import java.util.*;
class test
{
int Identify;
public static void main( String args[] )
{
Set set = new HashSet ();
System.out.println( set.add( "new" ) );
System.out.println( set.add( "new" ) );
System.out.println( set.add( new test(127) ) );
System.out.println( set.add( new test(127) ) );
}
test(int ID)
{
Identify = ID;
}
}
24.
import java.util.*;
class test implements Comparator < test >
{
int testNumber;
public static void main( String args[] )
{
Set < test > s1 = new TreeSet < test > ();
s1.add(new test());
s1.add(new test());
}
public int compare( test t1 , test t2 )
{
return t1.testNumber - t2.testNumber;
}
25.
import java.util.*;
import java.io.*;
26.
class test
{
public static void main( String args[] )
{
test( new int[] { 1 , 2 } );
}
a. Prints int[]
b. Prints int…
c. Compile error
d. Runtime error
e. None of the above
27.
class face
{
public void meth()
{
System.out.println("hello");
}
}
class test
{
public static void main( String args[] )
{
seth( new face(){} ); // 1
}
a. Compilations fails at 2
b. Compilations fails at 1
c. Runtime error at 2
d. Prints hello after successfully compiling.
e. Compilations fails at 1 and 2.
28.
class test <T>
{
public static void main( String args[] )
{
new test<String>().meth("hello"); // 1
}
29.
a. 1
b. 2
c. 3
d. Compiles without errors.
e. Compiles without errors. But Runtime errors exist.
30.
This code…
a. Will compile
b. Will not Compile at Line 1
c. Has Runtime errors.
d. Will not Compile at Line 2
e. Will not Compile at Line 1 and 2.
31.
32.
a. 1
b. 2
c. 3
d. 4
e. 5
33.
Which of the following modifiers can be used with a method local inner class ?
a. static
b. abstract
c. final
d. private
e. protected
f. public
g. default
34.
a. Prints 10
b. Prints 0
c. Compile error
d. Runtime error
e. None of the above
35.
a. Prints false
b. Prints true
c. Compile error at 1
d. Runtime error at 1
e. None of the above
36.
}
}
a. Compile error at 1
b. Runtime error at 1
c. Compiles without errors
d. Will compile if Line one includes a cast for face.
e. None of the above.
37.
List <? super String> list = new ArrayList <String > (); // 1
list.add(new Object()); // 2
Drag and Drop the following options over Line 1 and Line 2. If the Line will not compile drag
option a over the line. If it will compile, but will throw Exceptions drag b over the line. Drag c over
the line if both a and b do not fit the description.
38.
class test
{
public static void main( String... args )
{
Byte b = new Byte(1);
System.out.println("" + b + "20" );
}
}
a. Compile error
b. Runtime Exceptions are thrown.
c. Prints 120
d. Prints 12
e. None of the above.
39.
a. Prints 2
b. Prints 0
c. Compile error
d. Runtime error
e. None of the above
40.
class superb
{
int x=1;
static int x2=2;
void meth(){System.out.println("non static Superb");}
static void meth2(){System.out.println("static Superb");}
}
Drag and Drop the 9 output literals in the order in which they appear. Note that options can be
used more than once. ( Imagine nine empty spaces on the right hand side of these options).
a. 3 4
b. 1 2
c. non static test
d. static test
e. non static Superb
f. static Superb
41
package packed;
public class pack
{
static public int x1 = 7;
static protected int x2 = 8;
static int x3=9;
static private int x4 =10;
}
import packed.pack;
class test extends pack
{
public static void main( String args[] )
{
pack p = new pack();
System.out.println( pack.x2 );
}
}
a. Prints 8
b. Compile error
c. Runtime error
d. Prints nothing
e. None of the above.
42.
int x=1;
assert (x=2) : x;
This code…
43.
44.
a. Prints 0 1 2 3
b. Prints 0 0 0 0
c. Prints 0 0 3 0
d. Prints 0 2 0 0
e. Compile error
f. Runtime Exception occurs.
45.
import java.util.*;
class test implements Comparator<test>
{
private int x;
test(int input) { x = input; }
public static void main( String args[] )
{
List list = new ArrayList();
list.add(new test(2));
list.add(new test(2));
Collections.sort(list);
}
public int compare( test t1 , test t2 )
{
return t1.x - t2.x;
}
}
This code…
46.
47.
a. Prints 1 2 3 4
b. Prints 4 3 2 1
c. Prints nothing
d. Compile error
e. Runtime error
48.
Drag and drop the following options over the code fragments 1 and 2. One option may be used
more than once.
Options
a. Compiles
b. Does not compile.
49.
50.
class test
{
int x; test(int input) { x = input; }
public static void main( String args[] )
{
System.out.println( new test(3).equals(new test(3)) );
}
public boolean equals( Object o)
{
return ( (test)o ).x == x;
}
}
a. Prints true
b. Prints false
c. Compile error
d. Runtime Exceptions are encountered
e. None of the above.
51.
a. Prints 1
b. Prints 2
c. If Line 1 is replaced with return true; then m.size() returns 1.
d. If hashCode() is not overridden then regardless of whether line 1 returns true or false 2
will be printed on the screen.
e. Compile time error
f. Runtime exception is encountered.
g. If Line 1 is replaced with return true; then size() will not return any number greater than 1.
52.
a. Compile error
b. Runtime Exception
c. Prints true
d. Prints false
53.
a. sleep
b. join
c. yield
d. wait
e. notify
f. notifyAll
54.
Given these two classes and that you want to promote better coupling between them. What
changes are required to achieve good coupling ? Also given that the int variable main is to be
used when initializing coupler’s variables.
class test
{
int main=10;
public static void main ( String... blah )
{
coupler c = new coupler();
c.setMain( c.getMain() ); // 1
}
int getMain()
{ return main; }
}
class coupler
{
int pain;
void setMain(int mane)
{
pain = mane;
}
int getMain()
{ return new test().main; } // 2
}
55.
a. Compile error
b. Runtime error
c. Prints true true
d. Prints true false
e. Prints false true
f. Prints false false.
56.
import java.util.regex.*;
class test
{
public static void main( String args[] )
{
search("asf jgds8 93 qn" , "\\d\\w"); // 1
search("asf jgds8 93 qn" , ".*\\d"); // 2
search("asf jgds8 93 qn" , "\\d.*"); // 3
search("asf jgds8 93 qn" , "\\w.*\\d"); // 4
}
public static void search(String mat , String pat)
{
Pattern p = Pattern.compile(pat);
Matcher m = p.matcher(mat);
while(m.find())
{
System.out.println(m.group());
}
}
}
a.
93
asf jgds8 93
8 93 qn
asf jgds8 93
b.
93
asf jgds8 93
8 93 qn
asf jgds8
c.
93
asf jgds8 93
asf jgds8 93
asf jgds8
57.
Which of the following are true about the meta characters used with the regex package ?
58.
class test
{
public static void main ( String [] blah )
{
System.out.printf("%s", new test());
}
public String toString()
{
return "testing something";
}
59.
class test
{
public static void main ( String [] blah )
{
System.out.printf("%1$b", "123");
}
public String toString()
{
return "testing something";
}
}
a. Prints false
b. Prints true
c. Runtime Exception
d. Compile error
e. None of tha above.
60.
class sup
{
static void method(){System.out.println("Super");} // 1
}
What class modifier(s) can be inserted at line 1 to prevent method() from being overridden
(hidden ) without causing compile time errors ?
a. final
b. private
c. protected
d. default
e. transient
f. Hiding cannot be prevented.
61.
class sup
{ void method() throws Exception {} }
62.
enum num
{BERT("CAFE") , KATHY("BABE")}
63.
a. Prints 2
b. Prints 0
c. Does not compile
d. Runtime Exception.
e. None of the above.
64.
class test
{
public static void main( String args[] )
{
class ma
{
final int x;
ma ()
{
x = 10;
System.out.println( this.x);
}
}
new ma();
}
}
What is the output ?
a. Fails to compile
b. Runtime Exceptions are encountered
c. Prints 10
d. Prints 0
e. None of the above.
65.
a. 321
b. 3121
c. 3121f
d. 321f
e. This fragment will not compile
f. This fragment will throw an IndexOutOfBoundsException
g. None of the above.
66.
a. String #baby;
b. char c123;
c. Byte $wombat;
d. Long long;
e. Short ~english;
67.
class test
{
public static void main( String args[] )
{
new test().method((short)1);
}
void method(int... i)
{
System.out.println("int");
}
void method(Integer i)
{
System.out.println("pint");
}
void method(byte i)
{
System.out.println("bite");
}
}
What is printed ?
a. pint
b. bite
c. int
d. Nothing is printed
e. None of the above.
68.
69.
class test
{
test tester;
test( test t )
{
tester = t;
}
test()
{}
public static void main( String args[] )
{
test t = null;
t = new test();
t = new test(t);
t = null; // 1
}
}
How many objects are eligible for garbage collection after line //1 executes ?
a. One
b. Two
c. Zero
d. This code will not compile
e. None of the above.
70.
class test
{
test()
{
try
{
throw new RuntimeException();
}
finally
{
System.out.println("Damn !");
}
}
public static void main( String args[] )
{
try
{
new test();
}
catch ( Throwable t )
{
System.out.println("Caught");
}
}
}
a. Damn ! RuntimeException.
b. Damn ! Caught RuntimeException
c. RuntimeException caught
d. Damn ! Caught
e. Caught.
f. None of the above.
71.
Which of the following are valid declarations of a for in loop that loops through an int array named
j?
a. foreach ( int i : j ) {}
b. for( int I : []j ) {}
c. for ( int I : j ) {}
d. for ( int I : j[] ) {}
e. None of the above.
72.
Drag and drop the correct relation that is exhibited by this class. Drag the correct option on the
class definition. Ignore the class names and member variable names. Choose the option based
on is a and has a relationships.
a. 1
b. 2
c. 3
d. No compile errors
e. Runtime error.
2.
public <T> List<T> meth(List<?> type)
{
System.out.println(type); // 1
return new ArrayList<String>(); // 2
}
This code…
a. Will compile
b. Will not Compile at Line 1
c. Has Runtime errors.
d. Will not Compile at Line 2
e. Will not Compile at Line 1 and 2.
3.
public <T> List<?> meth(List<T> type)
{
System.out.println(type); // 1
return new ArrayList<String>(); // 2
}
a. Will compile
b. Will not Compile at Line 1
c. Has Runtime errors.
d. Will not Compile at Line 2
e. Will not Compile at Line 1 and 2.
4.
a. Will compile
b. Will not Compile at Line 1
c. Has Runtime errors.
d. Will not Compile at Line 2
e. Will not Compile at Line 1 and 2.
5.
import java.util.*;
class test
{
List<? super String> tester = new ArrayList < String >();
public static void main( String args[] )
{
seth( tester ); // 2
}
6.
List <? extends String> list = new ArrayList <String > ();
for ( Object o : list )
{
System.out.println(o);
}
a. Compile error
b. Runtime error
c. Nothing can be added to list
d. Only a String reference can point to list. Hence a String reference is required in the loop.
e. None of the above.
7.
List <String> list = new ArrayList <String>; // 1
list.add("hello"); // 2
list.add("My dear"); // 3
for ( Object o : list ) // 4
{
System.out.println(o); // 5
}
1.
int []arr = new int[]{1,2,3,4} ;
int []arr2 = new int[]{new Integer(1),2,3,4};
System.out.println( Arrays.equals( arr,arr2 ));
a. Prints true
b. Prints false
c. Prints nothing
d. Compile error
e. Runtime error.
a. Prints 1 2 3 4
b. Prints nothing
c. Compile error
d. Runtime error
e. None of the above