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

JAVA QUESTIONS 1 (24.1.22) : A. 1, 2, 4 B. 2, 4, 5 C. 2, 3, 4 D. All Are Correct. Answer: Option A

The document discusses Java questions related to arrays, exceptions, inheritance, interfaces, threads and assertions. It provides code examples and explanations for 20 multiple choice questions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

JAVA QUESTIONS 1 (24.1.22) : A. 1, 2, 4 B. 2, 4, 5 C. 2, 3, 4 D. All Are Correct. Answer: Option A

The document discusses Java questions related to arrays, exceptions, inheritance, interfaces, threads and assertions. It provides code examples and explanations for 20 multiple choice questions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

JAVA QUESTIONS 1(24.1.

22)
1.Which three are legal array declarations?
int [] myScores [];
char [] myChars;
int [6] myScores;
Dog myDogs [];
Dog myDogs [7];
A. 1, 2, 4

B. 2, 4, 5

C. 2, 3, 4

D. All are correct.


Answer: Option A

2.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};

C. int a [] = new int[5];

D. int [5] array;


Answer: Option B

3.Which three are valid declarations of a char?


1. char c1 = 064770;
2. char c2 = 'face';
3. char c3 = 0xbeef;
4. char c4 = \u0022;
5. char c5 = '\iface';
6. char c6 = '\uface';
A. 1, 2, 4

B. 1, 3, 6

C. 3, 5

D. 5 only
Answer: Option B
4.Which two are equal?
1. 32/4
2. (8 >> 2) << 4
3. 2^5
4. 128 >>> 2
5. 2 >> 5
A. 1 and 2

B. 2 and 4

C. 1 and 3

D. 2 and 3
Answer: Option B

5.What will be the output of the program?


public class Test
{
public static void aMethod() throws Exception
{
try /* Line 5 */
{
throw new Exception(); /* Line 7 */
}
finally /* Line 9 */
{
System.out.print("finally "); /* Line 11 */
}
}
public static void main(String args[])
{
try
{
aMethod();
}
catch (Exception e) /* Line 20 */
{
System.out.print("exception ");
}
System.out.print("finished"); /* Line 24 */
}
}

A. finally

B. exception finished

C. finally exception finished


D. Compilation fails
Answer: Option C
6.

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

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.Which constructs an anonymous inner class instance?


A. Runnable r = new Runnable() { };

B. Runnable r = new Runnable(public void run() { });

C. Runnable r = new Runnable { public void run(){}};

D. System.out.println(new Runnable() {public void run() { }});


Answer: Option D

9.What will be the output of the program?


public class TestObj
{
public static void main (String [] args)
{
Object o = new Object() /* Line 5 */
{
public boolean equals(Object obj)
{
return true;
}
} /* Line 11 */

System.out.println(o.equals("Fred"));
}
}

A. It prints "true".

B. It prints "Fred".

C. An exception occurs at runtime.

D. Compilation fails
Answer: Option D

10.

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?


A. After line 7

B. After line 8

C. After the start() method completes

D. When the instance running this code is made eligible for garbage collection.
Answer: Option D

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

12.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
Answer: Option D

13.What will be the output of the program?


public class Test
{
public int aMethod()
{
static int i = 0;
i++;
return i;
}
public static void main(String args[])
{
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}

A. 0

B. 1

C. 2

D. Compilation fails.
Answer: Option D

14.Which two statements are true for any concrete class implementing the java.lang.Runnable
interface?
1. You can extend the Runnable interface as long as you override the public run() method.
2. The class must contain a method called run() from which all code for that thread will be
initiated.
3. The class must contain an empty public void method named run().
4. The class must contain a public void method named runnable().
5. The class definition must include the words implements Threads and contain a method
called run().
6. The mandatory method must be public, with a return type of void, must be called run(),
and cannot take any arguments.
A. 1 and 3

B. 2 and 4

C. 1 and 5

D. 2 and 6
Answer: Option D

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

17.What will be the output of the program?


public class Test
{
public static void main (String args[])
{
String str = NULL;
System.out.println(str);
}
}

A. NULL

B. Compile Error

C. Code runs but no output

D. Runtime Exception
Answer: Option B

18.What two statements are true about properly overridden hashCode() and equals() methods?

1. hashCode() doesn't have to be overridden if equals() is.


2. equals() doesn't have to be overridden if hashCode() is.
3. hashCode() can always return the same value, regardless of the object that invoked it.
4. equals() can be true even if it's comparing different objects.
A. 1 and 2

B. 2 and 3

C. 3 and 4

D. 1 and 3
Answer: Option C

19.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
Answer: Option B

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

You might also like