Which of the following is a feature of Java 8 that allows functional programming?
Answer: A
Lambda expressions were introduced in Java 8, enabling functional programming. Syntax: (parameters) -> expression or statement
Q.62Hard
What will be the output? class Test { public static void main(String[] args) { int x = 10; { int x = 20; System.out.println(x); } System.out.println(x); } }
Answer: B
A block scope creates a new variable x = 20 which shadows the outer x. First print outputs 20, then the block ends, outer x (10) is printed.
Q.63Hard
Which of the following will throw a NullPointerException? String s = null; System.out.println(s.length());
Answer: C
Calling a method on a null reference throws NullPointerException. s.length() tries to invoke method on null, causing the exception.
Q.64Hard
Consider: class Parent { Parent() { System.out.println("P"); } } class Child extends Parent { Child() { System.out.println("C"); } } public static void main(String[] args) { new Child(); }
Answer: C
Child constructor implicitly calls super() first, executing Parent constructor. Output: Parent prints 'P', then Child prints 'C'.
Q.65Hard
What is the output of: int x = 5; System.out.println(x++ + ++x);
Answer: B
x++ uses current value (5) then increments, ++x increments first then uses value (7). Order of evaluation: 5 + 7 = 12. However, x becomes 7 after ++x.
Advertisement
Q.66Easy
Which keyword is used to create a constant variable in Java that cannot be modified after initialization?
Answer: C
The 'final' keyword is used to declare constants in Java. Once a final variable is assigned a value, it cannot be changed. 'const' is a reserved keyword but not used in Java.
Q.67Easy
What will be the result of: int a = 10; int b = 20; System.out.println(a + b + "Java");?
Answer: A
When + operator is used with numeric values first, they are added (10 + 20 = 30), and then concatenated with the string "Java", resulting in "30Java".
Q.68Medium
What is the purpose of the 'this' keyword in Java?
Answer: B
'this' is a reference variable that refers to the current object instance. It is used to distinguish instance variables from local variables with the same name, or to pass the current object as a parameter.
Q.69Easy
Consider the following code: int x = 5; x += 3; System.out.println(x); What is the output?
Answer: B
The += operator is an assignment operator that adds the right operand to the left operand and assigns the result. So x += 3 means x = x + 3, which is 5 + 3 = 8.
Q.70Medium
Which of the following correctly demonstrates method overloading?
Answer: B
Method overloading allows multiple methods with the same name but different parameters (number, type, or order) in the same class. This is a way to implement compile-time (static) polymorphism.
Q.71Easy
What is the size of 'long' data type in Java?
Answer: C
In Java, the 'long' data type is always 64 bits, regardless of the platform. This is one of Java's strengths - its size specifications are platform-independent.
Q.72Medium
Which of the following statements about static methods is correct?
Answer: C
Static methods belong to the class, not to any instance. They are called using the class name (e.g., ClassName.methodName()). They cannot directly access instance variables and cannot be overridden (though they can be hidden).
Q.73Medium
What will happen if you try to access an index out of bounds in an array?
Answer: C
Accessing an array index that doesn't exist throws an ArrayIndexOutOfBoundsException at runtime. This is a checked exception that must be handled or declared.
Q.74Medium
Which access modifier allows a member to be accessible only within the same package?
Answer: D
When no access modifier is specified, it's called package-private or default access. Members with default access are accessible only within the same package, not from other packages.
Q.75Easy
What is the output of: System.out.println(310);?
Answer: C
When dividing two integers, Java performs integer division and returns an integer result. 310 = 3 (remainder is discarded). To get decimal result, at least one operand should be a float or double.
Q.76Easy
Which of the following is true about the 'break' statement?
Answer: C
'break' statement exits the innermost loop or switch statement. It doesn't skip the current iteration (that's 'continue') and doesn't terminate the entire program.
Q.77Medium
What is the difference between 'break' and 'continue' in loop statements?
Answer: B
'break' immediately exits the loop, while 'continue' skips the remaining statements in the current iteration and jumps to the next iteration.
Q.78Medium
Which statement about constructors is INCORRECT?
Answer: B
Constructors do NOT have a return type, not even void. They automatically initialize objects when created. They can be overloaded and a class can have multiple constructors with different parameters.
Q.79Hard
What will be the output of: int x = 5; System.out.println(++x + x++);?
Answer: B
++x increments x to 6 and returns 6 (pre-increment). Then x++ returns 6 and increments x to 7 (post-increment). So 6 + 6 = 12. The order of evaluation in expressions like this depends on operator precedence and associativity.
Q.80Medium
Which of the following correctly represents variable declaration and initialization in Java?
Answer: C
Option C is correct. Option A causes a compilation error (cannot assign double to int). Option B works but there's implicit conversion. Option D causes a compilation error (cannot assign int to String).