Which of the following is the correct syntax to declare a variable in Java?
Answer: A
Java uses the syntax 'datatype variableName = value;' for variable declaration.
Q.2Easy
Which keyword is used to create a constant variable in Java?
Answer: C
The 'final' keyword is used to declare constants in Java. Once assigned, the value cannot be changed.
Q.3Easy
What will be the output of the following code? int x = 5; x += 3; System.out.println(x);
Answer: C
The += operator adds the right operand to the left operand. x += 3 is equivalent to x = x + 3, so 5 + 3 = 8.
Q.4Medium
Which of the following is NOT a primitive data type in Java?
Answer: C
String is a non-primitive (reference) data type in Java. Primitive types include int, boolean, double, float, char, long, short, byte.
Q.5Medium
What is the size of 'char' data type in Java?
Answer: B
The 'char' data type in Java is 2 bytes (16 bits) because it uses Unicode character encoding.
Advertisement
Q.6Medium
Which of the following will compile without error? public static void main(String args[]) { int x = 10; final int y = 20; y = 30; }
Answer: B
Once a final variable is assigned, it cannot be reassigned. The line 'y = 30;' will cause a compilation error.
Q.7Medium
What is the output of: System.out.println(310);
Answer: B
When both operands are integers, the division result is an integer. 310 = 3 (integer division, not floating-point).
Q.8Medium
Which operator has the highest precedence in Java?
Answer: C
Parentheses () and array access [] have the highest precedence in Java. Multiplication, division, and modulo come next, followed by addition and subtraction.
Q.9Medium
What is the output of: System.out.println(5 > 3 ? 'A' : 'B');
Answer: A
The ternary operator (?:) evaluates the condition (5 > 3, which is true) and returns the first value 'A'.
Q.10Medium
Which statement is correct about Java variables?
Answer: A
Java variable names are case-sensitive. 'age' and 'Age' are different variables. Names cannot start with digits, cannot contain spaces, and special characters are not allowed.
Q.11Hard
Consider the code: int a = 5; int b = a++; System.out.println(a + " " + b); What is the output?
Answer: B
The post-increment operator (a++) returns the value before incrementing. So b = 5, then a becomes 6. Output: 6 5
Q.12Hard
What will be printed? System.out.println(10 + 20 + "Java");
Answer: A
String concatenation works left to right. 10 + 20 = 30 (integer addition), then 30 + "Java" = "30Java" (string concatenation).
Q.13Medium
Identify the output: System.out.println("Java".length());
Answer: A
The length() method returns the number of characters in the string "Java", which is 4 characters.
Q.14Hard
What is the scope of a local variable in Java?
Answer: B
Local variables have scope limited to the method or code block in which they are declared. They cannot be accessed outside that block.
Q.15Medium
Which of the following is the correct way to declare and initialize an array in Java?
Answer: B
The correct syntax is 'datatype[] arrayName = {elements};' or 'datatype[] arrayName = new datatype[size];'
Q.16Easy
What will be the output of: boolean flag = true; System.out.println(!flag);
Answer: B
The logical NOT operator (!) inverts the boolean value. !true becomes false.
Q.17Hard
Consider: int x = 0; System.out.println(x == 0 && x != 1); What is the output?
Answer: A
Both conditions are true: x == 0 (true) and x != 1 (true). The && (AND) operator returns true when both operands are true.
Q.18Easy
What will be the output of: System.out.println(25);
Answer: B
Integer division in Java results in an integer. 25 = 2 (remainder is discarded). To get 2.5, at least one operand must be float/double.
Q.19Easy
Which keyword is used to prevent a class from being inherited?
Answer: C
The 'final' keyword prevents a class from being extended/inherited. A final class cannot have subclasses.
Q.20Easy
Which of the following is a valid variable name in Java?
Answer: B
Variable names must start with a letter, underscore (_), or dollar sign ($). They cannot start with digits, contain hyphens, or be reserved keywords.