Showing 41–50 of 100 questions
in Basics & Syntax
Consider: String s1 = "Java"; String s2 = new String("Java"); System.out.println(s1 == s2);
A
true
B
false
C
Compilation Error
D
Runtime Error
EXPLANATION
s1 is created in string pool, s2 is a new object in heap. Though content is same, references are different, so == returns false.
What is the difference between == and equals() in Java?
A
Both are identical
B
== compares values, equals() compares references
C
== compares references, equals() compares values
D
equals() is only for objects, == is for primitives
Correct Answer:
C. == compares references, equals() compares values
EXPLANATION
== compares object references (memory addresses). equals() method compares actual content/values and can be overridden.
Which statement about method parameters is correct?
A
Parameters are passed by reference in Java
B
Parameters are passed by value in Java
C
Objects are passed by reference, primitives by value
D
All parameters are passed by reference
Correct Answer:
B. Parameters are passed by value in Java
EXPLANATION
Java uses pass-by-value for all parameters. For objects, the value passed is the reference, but the reference itself is passed by value.
What is the output of: System.out.println(true && false || true);
A
true
B
false
C
null
D
Compilation Error
EXPLANATION
Evaluation: (true && false) || true = false || true = true. AND operator (&& has higher precedence than OR (||).
Which of the following will result in a compilation error? class Test { public void method1() { } public void method1(int x) { } }
A
Method overloading
B
Method overriding
C
No error, valid code
D
Syntax error
Correct Answer:
C. No error, valid code
EXPLANATION
This is valid method overloading. Two methods with same name but different parameters (method signature) is allowed in Java.
What is the scope of a variable declared inside a method in Java?
A
Global
B
Local
C
Static
D
Package-level
EXPLANATION
Variables declared inside a method have local scope and are only accessible within that method. They are destroyed when the method returns.
Which of the following is a valid method name in Java?
A
2ndMethod()
B
_myMethod()
C
my-method()
D
my method()
Correct Answer:
B. _myMethod()
EXPLANATION
Method names must start with a letter, underscore, or dollar sign. '_myMethod()' is valid. Names cannot start with digits, contain hyphens, or spaces.
Consider the code: int x = 5; x += 3; What is the value of x?
EXPLANATION
The compound assignment operator += means x = x + 3. So 5 + 3 = 8.
What will be the output of: System.out.println(10 / 3);
A
3.33
B
3
C
3.333333
D
Compilation Error
EXPLANATION
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.
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
Correct Answer:
B. To check if an object is an instance of a class
EXPLANATION
The 'instanceof' operator is used to check if an object is an instance of a specific class or implements a specific interface.