0% found this document useful (0 votes)
99 views10 pages

Java Questions 2: A. B. C. D

This document contains 25 multiple choice questions about Java concepts such as arrays, exceptions, threads, and object-oriented programming. The questions cover default values, keywords, array declaration and initialization, exception handling, inner classes, and more. Correct answers are provided for each question.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
99 views10 pages

Java Questions 2: A. B. C. D

This document contains 25 multiple choice questions about Java concepts such as arrays, exceptions, threads, and object-oriented programming. The questions cover default values, keywords, array declaration and initialization, exception handling, inner classes, and more. Correct answers are provided for each question.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

JAVA QUESTIONS 2

1.Which four options describe the correct default values for array elements of the types indicated?
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
Answer: Option B

2.Which one of these lists contains only Java programming language keywords?
A. class, if, void, long, Int, continue

B. goto, instanceof, native, finally, default, throws

C. try, virtual, throw, final, volatile, transient

D. strictfp, constant, super, implements, do

E. byte, break, assert, switch, include


Answer: Option B

3.Which will legally declare, construct, and initialize an array?


A. int [] myList = {"1", "2", "3"};

B. int [] myList = (5, 8, 2);

C. int myList [] [] = {4,9,7,0};

D. int myList [] = {4, 3, 7};


Answer: Option D

4.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]);
}

long [] fix(long [] a3)


{
a3[1] = 7;
return a3;
}
}

A. 12 15

B. 15 15

C. 345375

D. 375375
Answer: Option B

5.What will be the output of the program?


try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
System.out.println( "finished");

A. finished

B. Exception

C. Compilation fails.

D. Arithmetic Exception
Answer: Option C

6.Which is true about an anonymous inner class?


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. It can extend exactly one class or implement exactly one interface.

D. It can implement multiple interfaces regardless of whether it also extends a class.


Answer: Option C

7.

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?


A. after line 5

B. after line 6

C. after line 7

D. There is no way to be absolutely certain.


Answer: Option D

8.What is the value of "d" after this line of code has been executed?
double d = Math.round ( 2.5 + Math.random() );
A. 2

B. 3

C. 4

D. 2.5
Answer: Option B

9.Which of the following would compile without error?


A. int a = Math.abs(-5);

B. int b = Math.abs(5.0);

C. int c = Math.abs(5.5F);

D. int d = Math.abs(5L);
Answer: Option A

10.
interface Base
{
boolean m1 ();
byte m2(short s);
}

which two code fragments will compile?


1. interface Base2 implements Base {}
2. abstract class Class2 extends Base
{ public boolean m1(){ return true; }}
3. abstract class Class2 implements Base {}
4. abstract class Class2 implements Base
{ public boolean m1(){ return (7 > 4); }}
5. abstract class Class2 implements Base
{ protected boolean m1(){ return (5 > 7) }}
A. 1 and 2

B. 2 and 3

C. 3 and 4

D. 1 and 5
Answer: Option C

11.Which three form part of correct array declarations?


1. public int a [ ]
2. static int [ ] a
3. public [ ] int a
4. private int a [3]
5. private int [3] a [ ]
6. public final int [ ] a
A. 1, 3, 4

B. 2, 4, 5

C. 1, 2, 6

D. 2, 5, 6
Answer: Option C

12.

public class Test { }

What is the prototype of the default constructor?


A. Test( )
B. Test(void)

C. public Test( )

D. public Test(void)
Answer: Option C

13.

public void test(int x)


{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println( "odd");
}
else
{
System.out.println( "even");
}
}

Which statement is true?


A. Compilation fails.

B. "odd" will always be output.

C. "even" will always be output.

D. "odd" will be output for odd values of x, and "even" for even values.
Answer: Option A

14.

public class While


{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 */
{
System.out.print("x plus one is " + (x + 1)); /* Line 8 */
}
}
}

Which statement is true?


A. There is a syntax error on line 1.

B. There are syntax errors on lines 1 and 6.

C. There are syntax errors on lines 1, 6, and 8.


D. There is a syntax error on line 6.
Answer: Option D

15.Suppose that you would like to create an instance of a new Map that has an iteration order that is
the same as the iteration order of an existing instance of a Map. Which concrete implementation of
the Map interface should be used for the new instance?
A. TreeMap

B. HashMap

C. LinkedHashMap

D. The answer depends on the implementation of the existing instance.


Answer: Option C

16.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
Answer: Option C

17.What is the name of the method used to start a thread execution?


A. init();

B. start();

C. run();

D. resume();
Answer: Option B

18.Which two are valid constructors for Thread?


1. Thread(Runnable r, String name)
2. Thread()
3. Thread(int priority)
4. Thread(Runnable r, ThreadGroup g)
5. Thread(Runnable r, int priority)
A. 1 and 3

B. 2 and 4
C. 1 and 2

D. 2 and 5
Answer: Option C

19.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.

C. An AssertionError is thrown and finished is output.

D. An AssertionError is thrown with the message "assertion failed."


Answer: Option B

20.

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 */
}
}

What causes compilation to fail?


A. Line 5

B. Line 6

C. Line 12

D. Line 14
Answer: Option D

21.What will be the output of the program?


public class Test
{
public static int y;
public static void foo(int x)
{
System.out.print("foo ");
y = x;
}
public static int bar(int z)
{
System.out.print("bar ");
return y = z;
}
public static void main(String [] args )
{
int t = 0;
assert t > 0 : bar(7);
assert t > 1 : foo(8); /* Line 18 */
System.out.println( "done ");
}
}

A. bar

B. bar done

C. foo done

D. Compilation fails
Answer: Option D

22.What will be the output of the program?


public class Foo
{
Foo()
{
System.out.print("foo");
}

class Bar
{
Bar()
{
System.out.print("bar");
}
public void go()
{
System.out.print("hi");
}
} /* class Bar ends */

public static void main (String [] args)


{
Foo f = new Foo();
f.makeBar();
}
void makeBar()
{
(new Bar() {}).go();
}
}/* class Foo ends */

A. Compilation fails.

B. An error occurs at runtime.

C. It prints "foobarhi"

D. It prints "barhi"
Answer: Option C

23.

public class ExceptionTest


{
class TestException extends Exception {}
public void runTest() throws TestException {}
public void test() /* Point X */
{
runTest();
}
}

At Point X on line 5, which code is necessary to make the code compile?


A. No code is necessary.

B. throws Exception

C. catch ( Exception e )

D. throws RuntimeException
Answer: Option B

24.Which statement is true?


A. catch(X x) can catch subclasses of X where X is a subclass of Exception.

B. The Error class is a RuntimeException.

C. Any statement that can throw an Error must be enclosed in a try block.

D. Any statement that can throw an Exception must be enclosed in a try block.
Answer: Option A

25.Which statement is true?


A. A try statement must have at least one corresponding catch block.
B. Multiple catch statements can catch the same class of exception more than once.

An Error that might be thrown in a method must be declared as thrown by that method, or be
C.
handled within that method.

Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block
D.
will always start to execute.
Answer: Option D

You might also like