Choose Layout


Java Quiz
1.
What is the output of the following code.
public class Sample { public static void main(String[] args) { System.out.println(5 & 9); } }
Explaination
& is bitwise operator. 5 & 9 = 00000101 & 00001001 = 00000001 = 1.
2.
Which of the following code can be used to add two integers?
3.
What could be the output of following program?
import java.util.ArrayList; public class Sample { public static void main(String[] args) { ArrayListnumbers = new ArrayList (); numbers.add(1); numbers.add(2); numbers.add(3); int sum = 0; numbers.forEach(number ->{ sum = sum + number; }); System.out.println(sum); } }
Explaination
Compile Error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Local variable sum defined in an enclosing scope must be final or effectively final
4.
What is the output of following program?
public class Sample { public static void main(String[] args) { if(2) { System.out.println("true"); } else { System.out.println("false"); } } }
Explaination
Compile Error with the message "Type mismatch: cannot convert from int to boolean".
5.
Which of the following is not an inbuilt package?
6.
In the following program, code block 1 runs fine while code block 2 throws NullPointerException. Why?
public class Sample { public static void main(String[] args) { String s = null; // code block 1 if(s != null && s.length() < 10) { System.out.println(""); } else { System.out.println("s is null"); } // code block 2 if(s.length()>10) { System.out.println("s have length > 10"); } } }
Explaination
When using the conditional and and or operators (&& and ||), Java does not evaluate the second operand unless it is necessary to resolve the result. This allows statements like if (s != null && s.length() < 10) to work reliably. This is called Short Circuiting.
7.
Which of the following is preferred while programming with Threads
Explaination
StringBuffer is thread safe.
8.
Which of the Exception is thrown when the following code is run?
public class Sample { public static void main(String[] args) { String[] names = new String[3]; names[0] = "Sherlock"; names[1] = "Watson"; names[2] = "Mary"; System.out.println(names[4]); } }
Explaination
ArrayIndexOutOfBoundsException occurs when an element is accessed with an index greater than the size of array.
9.
Can constructor return a value?
Explaination
Constructor can never return a value. A constructor is called by the memory allocation and object initialization code in the Java Runtime. A user code should never expect a return value from constructor.
10.
Which is the final state in an Object Life-cycle?
Explaination
Java Object Life-cycle:
1. Created
2. In use (strongly reachable)
3. Invisible
4. Unreachable
5. Collected
6. Finalized
7. Deallocated
11.
Method Overriding is an example of
Explaination
It is only during runtime, the method specific to the object is run. Hence dynamic.
Reference
12.
Method Overloading is an example of
Explaination
Which method would be called is determined by the parameter list at the compile time. Resolving the methods to be called at Compile time is called Static Binding and while at runtime, is called Dynamic Binding.
Reference
13.
What is the output of the following program:
public class Main { public static void main(String[] args) { int sum = 0; for(int i=0;i<10;i++) { sum = i; } System.out.println(sum); } }
Explaination
Each time in the loop, sum is loaded with i value. The last time the loop is executed, i has a value of 9. Therefore sum=9 and 9 is printed to the console.
14.
Which of the following operator cannot be applied for integer operands?
Explaination
&& accepts only boolean values are operands. If && is applied on two integer operands, compilation error with the message \"The operator && is undefined for the argument type(s) int, int\" would be displayed.
15.
Which of the following is not a builtin datatype?
Explaination
Java Builtin Datatypes are: boolean, byte, char, short, int, long, float, double.
Reference
16.
What could be the output of the following program.
class Main { public static void main(String args[]) { System.out.println(getNum()); } public int getNum() { return 12; } }
Explaination
Java does not allow to call a non-static method from a static method without object reference.
17.
What is the value of i
that is printed?
public class Sample { public static void main(String[] args) { int i; System.out.println(i); } }
Explaination
Compile Error occurs with the following message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The local variable i may not have been initialized
18.
Which of the following is a valid keyword
Explaination
Java is statistically typed programming language. string is different from String. In Java we have String class, but no string. Similarly FLOAT and Try. throw is a valid keyword. throw is the keyword used to throw exceptions to the calling method.
19.
Is 'String[] args' mandatory in the definition of main method?
Explaination
Its an architecture decision taken by Java Designers.
Reference
Submit Your Quiz Question
Note: The question should not match any of the existing questions. And should not be copied from other websites.