Java
Java
1. Which four options describe the correct default values for array elements of the types
indicated?
1.
2.
3.
4.
5.
6.
int -> 0
String -> "null"
Dog -> null
char -> '\u0000'
float -> 0.0f
boolean -> true
A.
1, 2, 3, 4
B.
1, 3, 4, 5
C.
2, 4, 5, 6
D.
3, 4, 5, 6
2. Which one of these lists contains only Java programming language keywords?
A.
B.
C.
D.
E.
B.
C.
D.
A.
method
B.
native
C.
subclasses
D.
reference
E.
array
interface
B.
string
C.
Float
D.
unsigned
7.
A.
1, 2, 4
B.
2, 4, 5
C.
2, 3, 4
D.
final int k = 4;
public int k = 4;
static int k = 4;
abstract int k = 4;
volatile int k = 4;
protected int k = 4;
A.
1, 2 and 3
B.
2, 3 and 4
C.
3, 4 and 5
D.
4, 5 and 6
8. Which one of the following will declare an array and initialize it with five numbers?
A.
B.
int [] a = {23,22,21,20,19};
C.
D.
char c1 = 064770;
char c2 = 'face';
char c3 = 0xbeef;
char c4 = \u0022;
char c5 = '\iface';
char c6 = '\uface';
A.
1, 2, 4
B.
1, 3, 6
C.
3, 5
D.
5 only
B.
C.
D.
boolean b1 = 0;
B.
boolean b2 = 'false';
C.
boolean b3 = false;
D.
boolean b4 = Boolean.false();
E.
boolean b5 = no;
float f1 = -343;
float f2 = 3.14;
float f3 = 0x12345;
float f4 = 42e7;
float f5 = 2001.0D;
float f6 = 2.81F;
A.
1, 2, 4
B.
2, 3, 5
C.
1, 3, 6
D.
2, 4, 6
String s1 = null;
B.
String s2 = 'null';
C.
D.
-128 to 127
B.
-(215) to (215) - 1
C.
0 to 32767
D.
0 to 65535
INNER CLASSES
1. Which is true about an anonymous inner class?
2.
A.
It can extend exactly one class and implement exactly one interface.
B.
It can extend exactly one class and can implement multiple interfaces.
C.
D.
class Boo
{
Boo(String s) { }
Boo() { }
}
class Bar extends Boo
{
Bar() { }
Bar(String s) {super(s);}
void zoo()
{
// insert code here
}
}
which one create an anonymous inner class from within class Bar?
A.
B.
C.
D.
B.
C.
D.
B.
C.
D.
6.
A.
B.
C.
D.
class Foo
{
class Bar{ }
}
class Test
{
public static void main (String [] args)
{
Foo f = new Foo();
/* Line 10: Missing statement ? */
}
}
which statement, inserted at line 10, creates an instance of Bar?
7.
A.
B.
C.
D.
B.
C.
MyOuter.MyInner mi = m.new MyOuter.MyInner();
D.
2.
A.
public
B.
private
C.
protected
D.
transient
3.
A.
B.
C.
D.
interface Base
{
boolean m1 ();
byte m2(short s);
}
which two code fragments will compile?
1 and 2
B.
2 and 3
C.
3 and 4
D.
1 and 5
5.
public int a [ ]
static int [ ] a
public [ ] int a
private int a [3]
private int [3] a [ ]
public final int [ ] a
A.
1, 3, 4
B.
2, 4, 5
C.
1, 2, 6
D.
2, 5, 6
Test( )
B.
Test(void)
C.
public Test( )
D.
public Test(void)
6. What is the most restrictive access modifier that will allow members of one class to have
access to members of another class in the same package?
A.
public
B.
abstract
C.
protected
E.
default access
D.
synchronized
A.
1 and 3
B.
2 and 4
C.
1 only
D.
B.
C.
D.
E.
1 and 2
B.
2, 3 and 5
C.
3, 4, and 5
D.
2 and 4
10. You want a class to have access to members of another class in the same package. Which is
the most restrictive access that accomplishes this objective?
A.
public
B.
private
C.
protected
D.
default access
12.
A.
int
B.
byte
C.
long
D.
double
class A
{
protected int method1(int a, int b)
{
return 0;
}
}
Which is valid in a class that extends class A?
A.
B.
C.
D.
B.
C.
D.
14. Which two of the following are legal declarations for nonnested classes and interfaces?
1.
2.
3.
4.
5.
6.
A.
1 and 4
B.
2 and 5
C.
3 and 6
D.
4 and 6
15. Which of the following class level (nonlocal) variable declarations will not compile?
A.
protected int a;
B.
transient int b = 3;
C.
D.
volatile int d;
A.
2, 4
B.
3, 5
C.
4, 5
D.
1, 2
17. Given a method in a protected class, what access modifier do you use to restrict access to that
method to only the other members of the same class?
A.
final
B.
static
C.
private
D.
protected
E.
volatile
B.
C.
D.
EXCEPTIONS
1. What will be the output of the program?
public class Foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
A.
Finally
B.
Compilation fails.
C.
D.
System.out.println("finished");
A.
finished
B.
Exception
C.
Compilation fails.
D.
Arithmetic Exception
ABCD
B.
Compilation fails.
C.
D.
BD
B.
BCD
C.
BDE
D.
BCDE
}
public static void main(String [] args)
{
try
{
System.out.print("hello ");
throwit();
}
catch (Exception re )
{
System.out.print("caught ");
}
finally
{
System.out.print("finally ");
}
System.out.println("after ");
}
}
A.
B.
Compilation fails
C.
D.
finally
B.
exception finished
C.
D.
Compilation fails
AC
B.
BC
C.
ACD
D.
ABCD
AB
B.
BC
C.
ABC
D.
BCD
{
public static void main(String args[])
{
try
{
System.out.print("Hello world ");
}
finally
{
System.out.println("Finally executing ");
}
}
}
A.
Nothing. The program will not compile because no exceptions are specified.
B.
Nothing. The program will not compile because no catch clauses are specified.
C.
Hello world.
D.
}
A.
Ex0 caught
B.
exception caught
C.
D.
THREADS
1. What is the name of the method used to start a thread execution?
A.
init();
B.
start();
C.
run();
D.
resume();
A.
1 and 3
B.
2 and 4
C.
1 and 2
D.
2 and 5
4.
notify();
notifyAll();
isInterrupted();
synchronized();
interrupt();
wait(long msecs);
sleep(long msecs);
yield();
A.
1, 2, 4
B.
2, 4, 5
C.
1, 2, 6
D.
2, 3, 4
/* Missing code? */
}
public void run() {}
}
Which of the following line of code is suitable to start a thread ?
A.
B.
C.
D.
B.
C.
D.
start()
wait()
notify()
run()
terminate()
A.
1 and 4
B.
2 and 3
C.
3 and 4
D.
2 and 4
7. Which three guarantee that a thread will leave the running state?
1. yield()
2.
3.
4.
5.
6.
7.
wait()
notify()
notifyAll()
sleep(1000)
aLiveThread.join()
Thread.killThread()
A.
1, 2 and 4
B.
2, 5 and 6
C.
3, 4 and 7
D.
4, 5 and 7
wait()
B.
notify()
C.
notifyall()
D.
void run()
B.
C.
D.
run();
B.
start();
C.
stop();
D.
main();
run();
B.
construct();
C.
start();
D.
register();
12. Assume the following method is properly synchronized and called from a thread A on an
object B:
wait(2000);
After calling this method, when will the thread A become a candidate to get another turn at
the CPU?
A.
B.
C.
D.
13. Which of the following will not directly cause a thread to stop?
A.
notify()
B.
wait()
C.
InputStream access
D.
sleep()
14. Which class or interface defines the wait(), notify(),and notifyAll() methods?
15.
A.
Object
B.
Thread
C.
Runnable
D.
Class
new Runnable(MyRunnable).start();
B.
new Thread(MyRunnable).run();
C.
D.
new MyRunnable().start();
12 15
B.
15 15
C.
345375
D.
375375
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
}
A.
true true
B.
false true
C.
true false
D.
false false
slip stream
B.
slipstream stream
C.
D.
-2147483648 and 1
B.
C.
-2147483648 and -1
D.
1 and -2147483648
A.
true
B.
false
C.
Compilation fails
D.
small
B.
tiny
C.
huge
D.
Compilation fails
}
}
A.
52
B.
53
C.
63
D.
64
53
B.
82
C.
83
D.
85
}
A.
B.
C.
D.
14
ok
B.
dokey
C.
ok dokey
D.
No output is produced
E.
Compilation error
}
void start()
{
int a = 3;
int b = 4;
System.out.print(" " + 7 + 2 + " ");
System.out.print(a + b);
System.out.print(" " + a + b + " ");
System.out.print(foo() + a + b + " ");
System.out.println(a + b + foo());
}
String foo()
{
return "foo";
}
}
A.
9 7 7 foo 7 7foo
B.
72 34 34 foo34 34foo
C.
9 7 7 foo34 34foo
D.
72 7 34 foo34 7foo
77
B.
7 14
C.
14 0
D.
14 14
A.
null null 42
B.
0 0 42
C.
0 42 42
D.
000
count = 0
B.
count = 2
C.
count = 3
D.
count = 4
B.
C.
D.
16
TreeMap
B.
HashMap
C.
LinkedHashMap
D.
2. Which class does not override the equals() and hashCode() methods, inheriting them directly
from class Object?
A.
java.lang.String
B.
java.lang.Double
C.
java.lang.StringBuffer
D.
java.lang.Character
3. Which collection class allows you to grow or shrink its size and provides indexed access to
its elements, but whose methods are not synchronized?
A.
java.util.HashSet
B.
java.util.LinkedHashSet
C.
java.util.List
D.
java.util.ArrayList
4. You need to store elements in a collection that guarantees that no duplicates are stored and
all elements can be accessed in natural order. Which interface provides that capability?
A.
java.util.Map
B.
java.util.Set
C.
java.util.List
D.
java.util.Collection
Java.util.Map
B.
Java.util.List
C.
Java.util.HashTable
D.
Java.util.Collection
6. Which interface provides the capability to store objects using a key-value pair?
A.
Java.util.Map
B.
Java.util.Set
C.
Java.util.List
D.
Java.util.Collection
7. Which collection class allows you to associate its elements with key values, and allows you
to retrieve objects in FIFO (first-in, first-out) sequence?
A.
java.util.ArrayList
B.
java.util.LinkedHashMap
C.
java.util.HashMap
D.
java.util.TreeMap
8. Which collection class allows you to access its elements by associating a key with an
element's value, and provides synchronization?
A.
java.util.SortedMap
B.
java.util.TreeMap
C.
java.util.TreeSet
D.
java.util.Hashtable
10.
A.
float f = 1F;
B.
float f = 1.0;
C.
float f = "1";
D.
float f = 1.0d;
/* Missing Statement ? */
public class foo
{
public static void main(String[]args)throws Exception
{
java.io.PrintWriter out = new java.io.PrintWriter();
new java.io.OutputStreamWriter(System.out,true);
out.println("Hello");
}
}
What line of code should replace the missing statement to make this program compile?
A.
No statement required.
B.
import java.io.*;
C.
include java.io.*;
D.
import java.io.PrintWriter;
0 to 32767
B.
0 to 65535
C.
-256 to 255
D.
-32768 to 32767
run
import
default
implement
A.
1 and 2
B.
2 and 3
C.
3 and 4
D.
2 and 4
GARBAGE COLLECTIONS
1.
void start() {
A a = new A();
B b = new B();
a.s(b);
b = null; /* Line 5 */
a = null; /* Line 6 */
System.out.println("start completed"); /* Line 7 */
}
When is the B object, created in line 3, eligible for garbage collection?
2.
A.
after line 5
B.
after line 6
C.
after line 7
D.
class HappyGarbage01
{
public static void main(String args[])
{
HappyGarbage01 h = new HappyGarbage01();
h.methodA(); /* Line 6 */
}
Object methodA()
{
Object obj1 = new Object();
Object [] obj2 = new Object[1];
obj2[0] = obj1;
obj1 = null;
return obj2[0];
}
}
Where will be the most chance of the garbage collector being invoked?
A.
After line 9
B.
After line 10
3.
C.
After line 11
D.
class Bar { }
class Test
{
Bar doBar()
{
Bar b = new Bar(); /* Line 6 */
return b; /* Line 7 */
}
public static void main (String args[])
{
Test t = new Test(); /* Line 11 */
Bar newBar = t.doBar(); /* Line 12 */
System.out.println("newBar");
newBar = new Bar(); /* Line 14 */
System.out.println("finishing"); /* Line 15 */
}
}
At what point is the Bar object, created on line 6, eligible for garbage collection?
4.
A.
after line 12
B.
after line 14
C.
D.
class Test
{
private Demo d;
void start()
{
d = new Demo();
this.takeDemo(d); /* Line 7 */
} /* Line 8 */
void takeDemo(Demo demo)
{
demo = null;
demo = new Demo();
}
}
When is the Demo object eligible for garbage collection?
5.
A.
After line 7
B.
After line 8
C.
D.
When the instance running this code is made eligible for garbage collection.
public class X
{
public static void main(String [] args)
{
X x = new X();
X x2 = m1(x); /* Line 6 */
X x4 = new X();
x2 = x4; /* Line 8 */
doComplexStuff();
}
static X m1(X mx)
{
mx = new X();
return mx;
}
}
After line 8 runs. how many objects are eligible for garbage collection?
6.
A.
B.
C.
D.
6.
A.
B.
C.
D.
B.
C.
D.
7.
class X2
{
public X2 x;
public static void main(String [] args)
{
X2 x2 = new X2(); /* Line 6 */
X2 x3 = new X2(); /* Line 7 */
x2.x = x3;
x3.x = x2;
x2 = new X2();
x3 = x2; /* Line 11 */
doComplexStuff();
}
}
after line 11 runs, how many objects are eligible for garbage collection?
A.
B.
C.
D.
x.delete()
B.
x.finalize()
C.
Runtime.getRuntime().gc()
D.