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

Java Programming Quiz Questions

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views10 pages

Java Programming Quiz Questions

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA QUESTIONS 2

[Link] 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

[Link] 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

[Link] 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

[Link] will be the output of the program?


class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
[Link]();
}

void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
[Link](a1[ 0] + a1[1] + a1[2] + " ");
[Link](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

[Link] will be the output of the program?


try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
[Link]("Exception");
}
catch (ArithmeticException ae)
{
[Link](" Arithmetic Exception");
}
[Link]( "finished");

A. finished

B. Exception

C. Compilation fails.

D. Arithmetic Exception
Answer: Option C

[Link] 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 */
[Link]("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

[Link] is the value of "d" after this line of code has been executed?
double d = [Link] ( 2.5 + [Link]() );
A. 2

B. 3

C. 4

D. 2.5
Answer: Option B

[Link] of the following would compile without error?


A. int a = [Link](-5);

B. int b = [Link](5.0);

C. int c = [Link](5.5F);

D. int d = [Link](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

[Link] 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 */
{
[Link]( "odd");
}
else
{
[Link]( "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 */
{
[Link]("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

[Link] 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

[Link] class does not override the equals() and hashCode() methods, inheriting them directly
from class Object?
A. [Link]

B. [Link]

C. [Link]

D. [Link]
Answer: Option C

[Link] is the name of the method used to start a thread execution?


A. init();

B. start();

C. run();

D. resume();
Answer: Option B

[Link] 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

[Link] 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" ;
[Link]( "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

[Link] will be the output of the program?


public class Test
{
public static int y;
public static void foo(int x)
{
[Link]("foo ");
y = x;
}
public static int bar(int z)
{
[Link]("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 */
[Link]( "done ");
}
}

A. bar

B. bar done

C. foo done

D. Compilation fails
Answer: Option D

[Link] will be the output of the program?


public class Foo
{
Foo()
{
[Link]("foo");
}

class Bar
{
Bar()
{
[Link]("bar");
}
public void go()
{
[Link]("hi");
}
} /* class Bar ends */

public static void main (String [] args)


{
Foo f = new Foo();
[Link]();
}
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

[Link] 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

[Link] 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

Common questions

Powered by AI

In Java, the default values for array elements are as follows: int -> 0, String -> null, Dog -> null, char -> '\u0000', float -> 0.0f, boolean -> false. The correct answer from the options provided is B: int, Dog, char, and float .

In Java, the garbage collection mechanism identifies unreachable objects for memory cleanup. An object becomes eligible for garbage collection when there are no reachable references to it. In certain scenarios, such as within a method where objects are only referenced locally, it can be difficult to determine if and when garbage collection occurs without explicit reference analysis. Consequently, declarations beyond point nullifying are often uncertain until execution completes .

Valid constructors for the Thread class, such as 'Thread(Runnable r, String name)' and 'Thread()', facilitate creating Thread instances with specific run behavior ('Runnable r') or identification ('name'). Other variations like 'Thread(int priority)' are invalid as constructors don't set priorities directly, showing the need for additional methods like 'setPriority' .

If a Java class does not explicitly define any constructor, the compiler provides a default no-argument constructor with no operations. This implicit constructor is used during object creation unless overridden, ensuring that objects can still be instantiated even without explicit constructor definitions, which promotes flexibility but might limit precise control over object state initialization .

In Java, assertions are intended for internal invalid assumptions with an optional error message. A statement like 'assert expression;' is supported, with failures resulting in an AssertionError. Including a colon for messages provides clarity during assertion failures. In scenarios lacking direct message expressions, assertion failure simply throws 'AssertionError' without message details, impacting debugging ease and message clarity .

The expression 'Math.round(2.5 + Math.random())' evaluates the sum of 2.5 and a random number between 0.0 and 1.0, rounding the result to the nearest integer. Possible outcomes will always round to 3, as Math.random() returns values in the range [0.0, 1.0), so 2.5 + 0.0 rounds to 3, and closer to any closer integer above will still round to 3 .

Java's inheritance model allows classes like StringBuffer to inherit default implementations of equals() and hashCode() from the Object class. Without overriding, StringBuffer uses reference equality rather than content equality for equals() comparison, leading to distinct instances being unequal even if their contents are identical, impacting hashtable use and consistency .

In Java, if a specific exception is not caught by an earlier catch block due to its sequence or specificity, the program will show a compilation error if a general Exception precedes a specific ArithmeticException or similar. Compilation fails because catch blocks that catch a superclass exception precede subclasses, causing ambiguity without specific handling .

Correct array declarations in Java follow the form 'type[] arrayName', with variations allowing modifiers like 'public', 'private', 'static', etc. Faulty declarations like 'private int a[3]', implying fixed-size allocation as part of the declaration, contradict Java's dynamic array allocation principles; thus, are invalid .

Implementing a Map that maintains the same iteration order as an existing instance relies on using a LinkedHashMap, as it maintains insertion order. Alternatives like HashMap or TreeMap do not guarantee iteration order consistent with another Map, as HashMap does not maintain order and TreeMap sorts according to natural ordering or a custom comparator .

You might also like