0% found this document useful (0 votes)
193 views

Java Questions 1: A. B. C. D

This document contains 25 multiple choice questions about Java programming concepts. The questions cover topics like arrays, keywords, classes, methods, exceptions, threads, and more. Correct answers are provided for each question.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
193 views

Java Questions 1: A. B. C. D

This document contains 25 multiple choice questions about Java programming concepts. The questions cover topics like arrays, keywords, classes, methods, exceptions, threads, and more. Correct answers are provided for each question.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

JAVA QUESTIONS 1

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

2.Which is a reserved word in the Java programming language?


A. method

B. native

C. subclasses

D. reference

E. array
Answer: Option B

3.Which is a valid keyword in java?


A. interface

B. string

C. Float

D. unsigned
Answer: Option A

4.What will be the output of the program?


class PassS
{
public static void main(String [] args)
{
PassS p = new PassS();
p.start();
}

void start()
{
String s1 = "slip";
String s2 = fix(s1);
System.out.println(s1 + " " + s2);
}

String fix(String s1)


{
s1 = s1 + "stream";
System.out.print(s1 + " ");
return "stream";
}
}

A. slip stream

B. slipstream stream

C. stream slip stream

D. slipstream slip stream


Answer: Option D

5.What will be the output of the program?


public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (RuntimeException ex) /* Line 10 */
{
System.out.print("B");
}
catch (Exception ex1)
{
System.out.print("C");
}
finally
{
System.out.print("D");
}
System.out.print("E");
}
public static void badMethod()
{
throw new RuntimeException();
}
}

A. BD

B. BCD

C. BDE

D. BCDE
Answer: Option C
6.Which is true about a method-local inner class?
A. It must be marked final.

B. It can be marked abstract.

C. It can be marked public.

D. It can be marked static.


Answer: Option B

7.Which statement is true about a static nested class?


A. You must have a reference to an instance of the enclosing class in order to instantiate it.

B. It does not have access to nonstatic members of the enclosing class.

C. It's variables and methods must be static.

D. It must extend the enclosing class.


Answer: Option B

8.

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

C. after line 7, when doBar() completes

D. after line 15, when main() completes


Answer: Option B

9.Which of the following are valid calls to Math.max?


1. Math.max(1,4)
2. Math.max(2.3, 5)
3. Math.max(1, 3, 5, 7)
4. Math.max(-1.5, -2.8f)
A. 1, 2 and 4

B. 2, 3 and 4

C. 1, 2 and 3

D. 3 and 4
Answer: Option A

10.

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

11.Which statement is true given the following?


Double d = Math.random();
A. 0.0 < d <= 1.0

B. 0.0 <= d < 1.0

C. Compilation fail

D. Cannot say.
Answer: Option B

12.Which two statements are true about wrapper or String classes?


1. If x and y refer to instances of different wrapper classes, then the
fragment x.equals(y) will cause a compiler failure.
2. If x and y refer to instances of different wrapper classes, then x == y can sometimes be
true.
3. If x and y are String references and if x.equals(y) is true, then x == y is true.
4. If x, y, and z refer to instances of wrapper classes and x.equals(y) is true,
and y.equals(z) is true, then z.equals(x) will always be true.
5. If x and y are String references and x == y is true, then y.equals(x) will be true.
A. 1 and 2

B. 2 and 3

C. 3 and 4

D. 4 and 5
Answer: Option D

13.

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

14.

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

15.

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

16.

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
17.What will be the output of the program?
public class Test
{
public static void main (String[] args)
{
String foo = args[ 1];
String bar = args[ 2];
String baz = args[ 3];
System.out.println( "baz = " + baz); /* Line 8 */
}
}

And the command line invocation:


> java Test red green blue
A. baz =

B. baz = null

C. baz = blue

D. Runtime Exception
Answer: Option D

18.Which statement is true?


A. A static method cannot be synchronized.

B. If a class has synchronized code, multiple threads can still access the nonsynchronized code.

Variables can be protected from concurrent access problems by marking them with
C.
the synchronized keyword.

D. When a thread sleeps, it releases its locks.


Answer: Option B

19.Which two can be used to create a new Thread?


1. Extend java.lang.Thread and override the run() method.
2. Extend java.lang.Runnable and override the start() method.
3. Implement java.lang.Thread and implement the run() method.
4. Implement java.lang.Runnable and implement the run() method.
5. Implement java.lang.Thread and implement the start() method.
A. 1 and 2

B. 2 and 3

C. 1 and 4

D. 3 and 4
Answer: Option C
20.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 Assertion Error is thrown.

D. An Assertion Error is thrown and finished is output.


Answer: Option C

21.

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

B. just after line 6

C. just after line 7

D. just after line 8


Answer: Option C

22.

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

23.

Which of the following are legal lines of code?


1. int w = (int)888.8;
2. byte x = (byte)1000L;
3. long y = (byte)100;
4. byte z = (byte)100L;
A. 1 and 2

B. 2 and 3

C. 3 and 4

D. All statements are correct.


Answer: Option D

24.Which two statements are equivalent?


1. 16*4
2. 16>>2
3. 16/2^2
4. 16>>>2
A. 1 and 2

B. 2 and 4

C. 3 and 4

D. 1 and 3
Answer: Option B

25.What will be the output of the program?


public class Switch2
{
final static short x = 2;
public static int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 3; z++)
{
switch (z)
{
case x: System.out.print("0 ");
case x-1: System.out.print("1 ");
case x-2: System.out.print("2 ");
}
}
}
}

A. 012

B. 012122

C. 210100

D. 212012
Answer: Option D

What will be the output of the program?


public class If2
{
static boolean b1, b2;
public static void main(String [] args)
{
int x = 0;
if ( !b1 ) /* Line 7 */
{
if ( !b2 ) /* Line 9 */
{
b1 = true;
x++;
if ( 5 > 6 )
{
x++;
}
if ( !b1 )
x = x + 10;
else if ( b2 = true ) /* Line 19 */
x = x + 100;
else if ( b1 | b2 ) /* Line 21 */
x = x + 1000;
}
}
System.out.println(x);
}
}
A. 0

B. 1

C. 101

D. 111
Answer: Option C

You might also like