Which of the following is the correct syntax to declare a variable in Java?
Java uses the syntax 'datatype variableName = value;' for variable declaration.
Which keyword is used to create a constant variable in Java?
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);
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);
The logical NOT operator (!) inverts the boolean value. !true becomes false.
What will be the output of: System.out.println(25);
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.
Advertisement
Which keyword is used to prevent a class from being inherited?
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?
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);
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?
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));
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));
Math.max(5, 10) = 10, Math.min(3, 8) = 3. So 10 - 3 = 7.
Which of the following is NOT a valid Java identifier?
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?
The 'final' keyword prevents a method from being overridden by subclasses.
Which of the following statements about Java packages is correct?
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?
'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?
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?
The 'super' keyword is used to refer to parent class members and invoke parent class constructors.
What is the purpose of the 'instanceof' operator?
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(310);
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?
The compound assignment operator += means x = x + 3. So 5 + 3 = 8.