Which of the following is the correct syntax to declare a variable in Java?
A int x = 10; B int x: 10; C x int = 10; D int: x = 10;
Java uses the syntax 'datatype variableName = value;' for variable declaration.
Which keyword is used to create a constant variable in Java?
A const B constant C final D immutable
The 'final' keyword is used to declare constants in Java. Once assigned, the value cannot be changed.
What will be the output of the following code? int x = 5; x += 3; System.out.println(x);
A 3 B 5 C 8 D 53
The += operator adds the right operand to the left operand. x += 3 is equivalent to x = x + 3, so 5 + 3 = 8.
What will be the output of: boolean flag = true; System.out.println(!flag);
A true B false C !true D Compilation error
The logical NOT operator (!) inverts the boolean value. !true becomes false.
What will be the output of: System.out.println(2 5 );
A 2.5 B 2 C 2.0 D Error
Integer division in Java results in an integer. 2 5 = 2 (remainder is discarded). To get 2.5, at least one operand must be float/double.
Which keyword is used to prevent a class from being inherited?
A static B abstract C final D private
The 'final' keyword prevents a class from being extended/inherited. A final class cannot have subclasses.
Which of the following is a valid variable name in Java?
A 123abc B _myVar C my-Var D class
Variable names must start with a letter, underscore (_), or dollar sign ($). They cannot start with digits, contain hyphens, or be reserved keywords.
What will be printed? float f = 5.5f; System.out.println(f);
A 5.5 B 5.50 C Error D 5
The 'f' suffix indicates a float literal. The output will be 5.5 (trailing zeros are not shown in standard printing).
Which of the following is the correct syntax for a multi-line comment in Java?
A // This is a comment B /* This is a comment */ C <!-- This is a comment --> D # This is a comment
Multi-line comments in Java use /* */ syntax. Single-line comments use //. Other syntaxes are for different languages.
What will be the output: String s = "Hello"; System.out.println(s.charAt(1));
A H B e C l D o
charAt() uses 0-based indexing. Index 1 refers to the second character, which is 'e' in "Hello".
What will be the output of: System.out.println(Math.max(5, 10) - Math.min(3, 8));
A 2 B 5 C 7 D 15
Math.max(5, 10) = 10, Math.min(3, 8) = 3. So 10 - 3 = 7.
Which of the following is NOT a valid Java identifier?
A _variable123 B $myVar C 123variable D my_Var$
Java identifiers cannot start with a digit. Valid identifiers must start with a letter, underscore, or dollar sign.
Which keyword is used to prevent method overriding in Java?
A abstract B final C static D synchronized
The 'final' keyword prevents a method from being overridden by subclasses.
Which of the following statements about Java packages is correct?
A Package names must be in uppercase B Package names use dot notation for hierarchy C A single Java file can belong to multiple packages D Package statement must appear after import statements
Package names follow a hierarchical structure using dots (e.g., com.example.app). Package statement must be the first non-comment statement.
What is the correct way to declare a constant in Java?
A const int MAX = 100; B static int MAX = 100; C public static final int MAX = 100; D final static int MAX = 100;
'const' is not a Java keyword. The correct convention for constants is public static final. Option D also works but C follows the standard convention.
Which of the following is a valid variable declaration?
A int 2num; B int num-2; C int num$2; D int num.2;
Variable names can contain letters, digits, underscores, and dollar signs, but cannot start with a digit or contain hyphens or dots.
What does the 'super' keyword do in Java?
A Refers to the current object B Refers to the parent class C Creates a new instance D Marks a method as superior
The 'super' keyword is used to refer to parent class members and invoke parent class constructors.
What is the purpose of the 'instanceof' operator?
A To create new instances B To check if an object is an instance of a class C To compare object values D To delete object instances
The 'instanceof' operator is used to check if an object is an instance of a specific class or implements a specific interface.
What will be the output of: System.out.println(3 10 );
A 3.33 B 3 C 3.333333 D Compilation Error
Both operands are integers, so integer division is performed. Result is 3 (not 3.33). To get decimal, at least one operand must be float/double.
Consider the code: int x = 5; x += 3; What is the value of x?
A 5 B 8 C 3 D 53
The compound assignment operator += means x = x + 3. So 5 + 3 = 8.