Java Objective Questions
Java Objective Questions
1. int -> 0
2. String -> "null"
3. Dog -> null
4. char -> '\u0000'
5. float -> 0.0f
6. boolean -> true
A. 1, 2, 3, 4
B. 1, 3, 4, 5
C. 2, 4, 5, 6
D. 3, 4, 5, 6
View Answer Discuss in Forum Workspace Report
2. Which one of these lists contains only Java programming language keywords?
A. class, if, void, long, Int, continue
B. native
C. subclasses
D. reference
E. array
View Answer Discuss in Forum Workspace Report
B. string
C. Float
D. unsigned
View Answer Discuss in Forum Workspace Report
6. Which three are legal array declarations?
1. int [] myScores [];
2. char [] myChars;
3. int [6] myScores;
4. Dog myDogs [];
5. Dog myDogs [7];
A. 1, 2, 4
B. 2, 4, 5
C. 2, 3, 4
7.
public interface Foo
{
int k = 4; /* Line 3 */
}
B. 2, 3 and 4
C. 3, 4 and 5
D. 4, 5 and 6
View Answer Discuss in Forum Workspace Report
8. Which one of the following will declare an array and initialize it with five numbers?
A. Array a = new Array(5);
B. int [] a = {23,22,21,20,19};
B. 1, 3, 6
C. 3, 5
D. 5 only
View Answer Discuss in Forum Workspace Report
B. boolean b2 = 'false';
C. boolean b3 = false;
D. boolean b4 = Boolean.false();
E. boolean b5 = no;
View Answer Discuss in Forum Workspace Report
B. 2, 3, 5
C. 1, 3, 6
D. 2, 4, 6
View Answer Discuss in Forum Workspace Report
B. String s2 = 'null';
C. 0 to 32767
D. 0 to 65535
View Answer Discuss in Forum Workspace Report
Exercise :: Operators and Assignments - Finding the output
Operators and Assignments - Finding the output
Operators and Assignments - Pointing out the correct statements
15. What will be the output of the program?
class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
A. 12 15
B. 15 15
C. 3 4 5 3 7 5
D. 3 7 5 3 7 5
View Answer Discuss in Forum Workspace Report
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
A. true true
B. false true
C. true false
D. false false
View Answer Discuss in Forum Workspace Report
void start()
{
String s1 = "slip";
String s2 = fix(s1);
System.out.println(s1 + " " + s2);
}
A. slip stream
B. slipstream stream
A. -2147483648 and 1
C. -2147483648 and -1
D. 1 and -2147483648
View Answer Discuss in Forum Workspace Report
A. true
B. false
C. Compilation fails
A. small
B. tiny
C. huge
D. Compilation fails
View Answer Discuss in Forum Workspace Report
A. 52
B. 5 3
C. 6 3
D. 6 4
View Answer Discuss in Forum Workspace Report
A. 53
B. 8 2
C. 8 3
D. 8 5
View Answer Discuss in Forum Workspace Report
A. 0
B. 7
C. 8
D. 14
View Answer Discuss in Forum Workspace Report
A. ok
B. dokey
C. ok dokey
D. No output is produced
E. Compilation error
View Answer Discuss in Forum Workspace Report
24. What will be the output of the program?
class SC2
{
public static void main(String [] args)
{
SC2 s = new SC2();
s.start();
}
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
View Answer Discuss in Forum Workspace Report
void twice(int x)
{
x = x*2;
s = x;
}
}
A. 77
B. 7 14
C. 14 0
D. 14 14
View Answer Discuss in Forum Workspace Report
class PassO
{
public static void main(String [] args)
{
PassO p = new PassO();
p.start();
}
void start()
{
Two t = new Two();
System.out.print(t.x + " ");
Two t2 = fix(t);
System.out.println(t.x + " " + t2.x);
}
A. null null 42
B. 0 0 42
C. 0 42 42
D. 0 0 0
View Answer Discuss in Forum Workspace Report
void test()
{
if ( b[0] && b[1] | b[2] )
count++;
if ( b[1] && b[(++count - 2)] )
count += 7;
System.out.println("count = " + count);
}
}
A. count = 0
B. count = 2
C. count = 3
D. count = 4
View Answer Discuss in Forum Workspace Report
A. 2
B. 4
C. 8
D. 16
View Answer Discuss in Forum Workspace Report
Exercise :: Exceptions - Finding the output
Exceptions - Finding the output
Exceptions - Pointing out the correct statements
29. 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.
A. finished
B. Exception
C. Compilation fails.
D. Arithmetic Exception
View Answer Discuss in Forum Workspace Report
A. ABCD
B. Compilation fails.
A. BD
B. BCD
C. BDE
D. BCDE
View Answer Discuss in Forum Workspace Report
B. Compilation fails
A. finally
B. exception finished
D. Compilation fails
View Answer Discuss in Forum Workspace Report
34. What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod() {}
}
A. AC
B. BC
C. ACD
D. ABCD
View Answer Discuss in Forum Workspace Report
A. AB
B. BC
C. ABC
D. BCD
View Answer Discuss in Forum Workspace Report
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.
A. Ex0 caught
B. exception caught
B. It can extend exactly one class and can implement multiple interfaces.
38.
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. Boo f = new Boo(24) { };
43
public class MyOuter
{
public static class MyInner
{
public static void foo() { }
}
}
which statement, if placed in a class other than MyOuter or MyInner, instantiates an instance of
the nested class?
A. MyOuter.MyInner m = new MyOuter.MyInner();
B. after line 6
C. after line 7
Where will be the most chance of the garbage collector being invoked?
A. After line 9
B. After line 10
C. After line 11
46.
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?
A. after line 12
B. after line 14
47.
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();
}
}
B. After line 8
D. When the instance running this code is made eligible for garbage collection.
View Answer Discuss in Forum Workspace Report
48.
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?
A. 0
B. 1
C. 2
D. 3
View Answer Discuss in Forum Workspace Report
49.
public Object m()
{
Object o = new Float(3.14F);
Object [] oa = new Object[l];
oa[0] = o; /* Line 5 */
o = null; /* Line 6 */
oa[0] = null; /* Line 7 */
return o; /* Line 8 */
}
When is the Float object, created in line 3, eligible for garbage collection?
A. just after line 5
50
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. 0
B. 1
C. 2
D. 3
View Answer Discuss in Forum Workspace Report
51. What allows the programmer to destroy an object x?
A. x.delete()
B. x.finalize()
C. Runtime.getRuntime().gc()
B. 3
C. 4
D. 2.5
View Answer Discuss in Forum Workspace Report
B. int b = Math.abs(5.0);
C. int c = Math.abs(5.5F);
D. int d = Math.abs(5L);
View Answer Discuss in Forum Workspace Report
B. 2, 3 and 4
C. 1, 2 and 3
D. 3 and 4
View Answer Discuss in Forum Workspace Report
55
public class Myfile
{
public static void main (String[] args)
{
String biz = args[1];
String baz = args[2];
String rip = args[3];
System.out.println("Arg is " + rip);
}
}
Select how you would start the program to cause it to print: Arg is 2
A. java Myfile 222
B. java Myfile 1 2 2 3 4
C. java Myfile 1 3 2 2
D. java Myfile 0 1 2 3
View Answer Discuss in Forum Workspace Report
xercise :: Declarations and Access Control - General Questions
Declarations and Access Control - General Questions
Declarations and Access Control - Finding the output
Declarations and Access Control - Pointing out the correct statements
56 You want subclasses in any package to have access to members of a superclass. Which is the
most restrictive access that accomplishes this objective?
A. public
B. private
C. protected
D. transient
View Answer Discuss in Forum Workspace Report
57.
public class Outer
{
public void someOuterMethod()
{
//Line 5
}
public class Inner { }
public static void main(String[] argv)
{
Outer ot = new Outer();
//Line 10
}
}
58
interface Base
{
boolean m1 ();
byte m2(short s);
}
B. 2 and 3
C. 3 and 4
D. 1 and 5
View Answer Discuss in Forum Workspace Report
B. 2, 4, 5
C. 1, 2, 6
D. 2, 5, 6
View Answer Discuss in Forum Workspace Report
60
public class Test { }
B. Test(void)
C. public Test( )
D. public Test(void)
61 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
D. synchronized
E. default access
View Answer Discuss in Forum Workspace Report
B. 2 and 4
C. 1 only
B. 2, 3 and 5
C. 3, 4, and 5
D. 2 and 4
View Answer Discuss in Forum Workspace Report
65. 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
66 What is the widest valid returnType for methodA in line 3?
public class ReturnIt
{
returnType methodA(byte x, double y) /* Line 3 */
{
return (long)x / y * 2;
}
}
A. int
B. byte
C. long
D. double
View Answer Discuss in Forum Workspace Report
67..
class A
{
protected int method1(int a, int b)
{
return 0;
}
}
69. Which two of the following are legal declarations for nonnested classes and interfaces?
1. final abstract class Test {}
2. public static interface Test {}
3. final public class Test {}
4. protected abstract class Test {}
5. protected interface Test {}
6. abstract public class Test {}
A. 1 and 4
B. 2 and 5
C. 3 and 6
D. 4 and 6
View Answer Discuss in Forum Workspace Report
70 Which of the following class level (nonlocal) variable declarations will not compile?
A. protected int a;
B. transient int b = 3;
D. volatile int d;
View Answer Discuss in Forum Workspace Report
71. Which two cause a compiler error?
1. float[ ] f = new float(3);
2. float f2[ ] = new float[ ];
3. float[ ]f1 = new float[3];
4. float f3[ ] = new float[3];
5. float f5[ ] = {1.0f, 2.0f, 2.0f};
A. 2, 4
B. 3, 5
C. 4, 5
D. 1, 2
View Answer Discuss in Forum Workspace Report
72. 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
View Answer Discuss in Forum Workspace Report
73.. Which is a valid declaration within an interface? Exercise :: Declarations and Access
Control - General Questions
Declarations and Access Control - General Questions
Declarations and Access Control - Finding the output
Declarations and Access Control - Pointing out the correct statements
73.. You want subclasses in any package to have access to members of a superclass. Which is
the most restrictive access that accomplishes this objective?
A. public
B. private
C. protected
D. transient
View Answer Discuss in Forum Workspace Report
74.
public class Outer
{
public void someOuterMethod()
{
//Line 5
}
public class Inner { }
75
interface Base
{
boolean m1 ();
byte m2(short s);
}
B. 2 and 3
C. 3 and 4
D. 1 and 5
View Answer Discuss in Forum Workspace Report
B. 2, 4, 5
C. 1, 2, 6
D. 2, 5, 6
View Answer Discuss in Forum Workspace Report
77.
public class Test { }
B. Test(void)
C. public Test( )
D. public Test(void)
View Answer Discuss in Forum Workspace Report
A. public static short stop = 23;
78. 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
D. synchronized
E. default access
View Answer Discuss in Forum Workspace Report
B. 2 and 4
C. 1 only
B. 2, 3 and 5
C. 3, 4, and 5
D. 2 and 4
View Answer Discuss in Forum Workspace Report
82. 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
View Answer Discuss in Forum Workspace Report
83. What is the widest valid returnType for methodA in line 3?
public class ReturnIt
{
returnType methodA(byte x, double y) /* Line 3 */
{
return (long)x / y * 2;
}
}
A. int
B. byte
C. long
D. double
View Answer Discuss in Forum Workspace Report
84..
class A
{
protected int method1(int a, int b)
{
return 0;
}
}
86.. Which two of the following are legal declarations for nonnested classes and interfaces?
1. final abstract class Test {}
2. public static interface Test {}
3. final public class Test {}
4. protected abstract class Test {}
5. protected interface Test {}
6. abstract public class Test {}
A. 1 and 4
B. 2 and 5
C. 3 and 6
D. 4 and 6
View Answer Discuss in Forum Workspace Report
87 Which of the following class level (nonlocal) variable declarations will not compile?
A. protected int a;
B. transient int b = 3;
D. volatile int d;
View Answer Discuss in Forum Workspace Report
88. Which two cause a compiler error?
1. float[ ] f = new float(3);
2. float f2[ ] = new float[ ];
3. float[ ]f1 = new float[3];
4. float f3[ ] = new float[3];
5. float f5[ ] = {1.0f, 2.0f, 2.0f};
A. 2, 4
B. 3, 5
C. 4, 5
D. 1, 2
View Answer Discuss in Forum Workspace Report
89. 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
View Answer Discuss in Forum Workspace Report
90. Which is a valid declaration within an interface?
A. public static short stop = 23;
92.
switch(x)
{
default:
System.out.println("Hello");
}
B. 2 and 4
C. 3 and 5
D. 4 and 6
View Answer Discuss in Forum Workspace Report
93.
public void test(int x)
{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
}
D. "odd" will be output for odd values of x, and "even" for even values.
View Answer Discuss in Forum Workspace Report
94.
public class While
{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 */
{
System.out.print("x plus one is " + (x + 1)); /* Line 8 */
}
}
}
B. HashMap
C. LinkedHashMap
96. 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
View Answer Discuss in Forum Workspace Report
97. 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
View Answer Discuss in Forum Workspace Report
98.. 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
View Answer Discuss in Forum Workspace Report
B. Java.util.List
C. Java.util.HashTable
D. Java.util.Collection
View Answer Discuss in Forum Workspace Report
100. 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
View Answer Discuss in Forum Workspace Report
101. 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
View Answer Discuss in Forum Workspace Report
102. 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
View Answer Discuss in Forum Workspace Report
B. float f = 1.0;
C. float f = "1";
D. float f = 1.0d;
View Answer Discuss in Forum Workspace Report
104.
/* 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;
105. What is the numerical range of char?
A. 0 to 32767
B. 0 to 65535
C. -256 to 255
D. -32768 to 32767
View Answer Discuss in Forum Workspace Report
106.Which of the following are Java reserved words?
1. run
2. import
3. default
4. implement
A. 1 and 2
B. 2 and 3
C. 3 and 4
D. 2 and 4
View Answer Discuss in Forum Workspace Report
Exercise :: Threads - General Questions
Threads - General Questions
Threads - Finding the output
Threads - Pointing out the correct statements
107. What is the name of the method used to start a thread execution?
A. init();
B. start();
C. run();
D. resume();
View Answer Discuss in Forum Workspace Report
B. 2 and 4
C. 1 and 2
D. 2 and 5
View Answer Discuss in Forum Workspace Report
109. Which three are methods of the Object class?
1. notify();
2. notifyAll();
3. isInterrupted();
4. synchronized();
5. interrupt();
6. wait(long msecs);
7. sleep(long msecs);
8. yield();
A. 1, 2, 4
B. 2, 4, 5
C. 1, 2, 6
D. 2, 3, 4
View Answer Discuss in Forum Workspace Report
110.
class X implements Runnable
{
public static void main(String args[])
{
/* Missing code? */
}
public void run() {}
}
B. 2 and 3
C. 3 and 4
D. 2 and 4
View Answer Discuss in Forum Workspace Report
113. Which three guarantee that a thread will leave the running state?
1. yield()
2. wait()
3. notify()
4. notifyAll()
5. sleep(1000)
6. aLiveThread.join()
7. Thread.killThread()
A. 1, 2 and 4
B. 2, 5 and 6
C. 3, 4 and 7
D. 4, 5 and 7
View Answer Discuss in Forum Workspace Report
114. Which of the following will directly stop the execution of a Thread?
A. wait()
B. notify()
C. notifyall()
D. exits synchronized code
View Answer Discuss in Forum Workspace Report
115.. Which method must be defined by a class implementing the java.lang.Runnable interface?
A. void run()
B. start();
C. stop();
D. main();
View Answer Discuss in Forum Workspace Report
117. Which method registers a thread in a thread scheduler?
A. run();
B. construct();
C. start();
D. register();
View Answer Discuss in Forum Workspace Report
118. 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. After thread A is notified, or after two seconds.
B. wait()
C. InputStream access
D. sleep()
View Answer Discuss in Forum Workspace Report
120. Which class or interface defines the wait(), notify(),and notifyAll() methods?
A. Object
B. Thread
C. Runnable
D. Class
View Answer Discuss in Forum Workspace Report
121.
public class MyRunnable implements Runnable
{
public void run()
{
// some code here
}
}
B. new Thread(MyRunnable).run();
D. new MyRunnable().start();
View Answer Discuss in Forum Workspace Report
Exercise :: Assertions - Finding the output
Assertions - Finding the output
Assertions - Pointing out the correct statements
Assertions - Pointing out the errors
122. What will be the output of the program?
public class Test
{
public static void main(String[] args)
{
int x = 0;
assert (x > 0) ? "assertion failed" : "assertion passed" ;
System.out.println("finished");
}
}
A. finished
B. Compiliation fails.
123.
public class Test
{
public void foo()
{
assert false; /* Line 5 */
assert false; /* Line 6 */
}
public void bar()
{
while(true)
{
assert false; /* Line 12 */
}
assert false; /* Line 14 */
}
}
B. Line 6
C. Line 12
D. Line 14
View Answer Discuss in Forum Workspace Report
A. bar
B. bar done
C. foo done
D. Compilation fails
View Answer Discuss in Forum Workspace Report
125. What will be the output of the program (when you run with the -ea option) ?
public class Test
{
public static void main(String[] args)
{
int x = 0;
assert (x > 0) : "assertion failed"; /* Line 6 */
System.out.println("finished");
}
}
A. finished
B. Compilation fails.
C. An AssertionError is thrown.
126.
public class Test2
{
public static int x;
public static int foo(int y)
{
return y * 2;
}
public static void main(String [] args)
{
int z = 5;
assert z > 0; /* Line 11 */
assert z > 2: foo(z); /* Line 12 */
if ( z < 7 )
assert z > 4; /* Line 14 */
switch (z)
{
case 4: System.out.println("4 ");
case 5: System.out.println("5 ");
default: assert z < 10;
}
if ( z < 10 )
assert z > 4: z++; /* Line 22 */
System.out.println(z);
}
}
B. Line 12
C. Line 14
D. Line 22
View Answer Discuss in Forum Workspace Report