Which of the following is a valid method name in Java?
Answer: B
Method names must start with a letter, underscore, or dollar sign. '_myMethod()' is valid. Names cannot start with digits, contain hyphens, or spaces.
Q.22Easy
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.23Easy
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.24Easy
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.25Easy
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.
Advertisement
Q.26Easy
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.27Easy
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.28Easy
Which of the following is NOT a Java keyword?
Answer: C
'thread' is not a Java keyword. 'synchronized', 'volatile', and 'transient' are all valid Java keywords used for special purposes.
Q.29Easy
What is the output of: System.out.println(5 > 3 ? "Yes" : "No");?
Answer: C
This uses the ternary operator (conditional operator). The condition 5 > 3 is true, so it returns the value after the '?', which is "Yes".
Q.30Easy
Consider the following code snippet. What is the primary purpose of the 'final' keyword when applied to a class in Java?
Answer: B
The 'final' keyword when applied to a class prevents it from being extended or inherited. This is a fundamental OOP concept in Java used to restrict inheritance.
Q.31Easy
Which of the following statements about Java's garbage collection is TRUE?
Answer: C
Java's garbage collector automatically identifies and reclaims memory of objects that are no longer referenced. While System.gc() can be suggested, it's not guaranteed to execute immediately.
Q.32Easy
What will happen when you execute the following code?
int[] arr = new int[5];
System.out.println(arr[5]);
Answer: C
An array of size 5 has valid indices from 0 to 4. Accessing arr[5] throws ArrayIndexOutOfBoundsException at runtime because index 5 is out of bounds.
Q.33Easy
Consider the following code. What will be printed?
String s = "Java";
s = s.concat(" Programming");
System.out.println(s.length());
Answer: C
"Java" has 4 characters. After concatenation with " Programming" (12 characters including space), the total length is 4 + 13 = 17 characters.
Q.34Easy
Which of the following is a valid declaration of a two-dimensional array in Java that can store 3 rows and 4 columns of integers?
Answer: D
All three declarations are syntactically valid in Java and represent the same 2D array structure. The position of brackets doesn't matter for 2D array declaration - they all create a 3x4 integer array.