Showing 901–910 of 958 questions
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.
Which statement about Java's garbage collection is true?
A
Garbage collection is deterministic and happens at fixed intervals
B
Garbage collection is non-deterministic and may never run
C
Calling System.gc() guarantees immediate garbage collection
D
Objects are garbage collected as soon as they go out of scope
Correct Answer:
B. Garbage collection is non-deterministic and may never run
EXPLANATION
Garbage collection in Java is non-deterministic. System.gc() is just a request, not a guarantee. Objects are eligible for GC when unreachable, but actual collection timing varies.
Consider: class A { static int x = 5; } How many times is x initialized when you create multiple A objects?
A
Once per object creation
B
Once when the class is loaded
C
Multiple times depending on object count
D
Twice (once for class, once for object)
Correct Answer:
B. Once when the class is loaded
EXPLANATION
Static variables are initialized only once when the class is first loaded into memory, regardless of object creation.