🎯 OCA Java Practice test 3

10 Free Practice Questions for 1Z0-808 Certification
All 10 questions shown below with complete explanations

πŸ“š OCA Object Creation: Constructors, this, Pass-by-Value & Initialization Order

Why Master these questions?

These topics appear frequently across the OCA exam, especially in questions on Pass-by-value, Constructors, this keyword, Default constructors, Private constructors, Instance initializers, Static blocks.

πŸ’‘ Note: This page shows 10 carefully selected questions . Each question includes detailed explanations and matches the real OCA exam format.

πŸ“š Topics Covered in Practice Test 3

Question 1 EASY πŸ“Œ Pass-by-Value in Methods
What is the output of the following Java code?

public class MathSquare {
public static long square(int x) {
long y = x * (long) x;
x = -1;
return y;
}
public static void main(String[] args) {
int value = 9;
long result = square(value);
System.out.println(value);
}
}
A. -1
B. 9
C. 81
D. 0
E. Compilation error

βœ… Correct Answer: B

The square method takes an integer parameter x, computes its square, and stores it in y, but changing x to -1 inside the method does not affect the value variable in the main method because Java uses pass-by-value. Thus, System.out.println(value) prints the original value, which is 9. The method's return value (result) is not used in the print statement, so 81 is not printed.

πŸ“– Detailed Explanation:

Method square(int x) receives a copy of the argument value, because Java is pass-by-value.

Inside the method, x is modified (x = -1;), but this modifies only the local copy, not the original value in main.

The method returns y, which is 9 * 9 = 81, but this returned value is stored in result, not printed.

The final print statement prints only System.out.println(value);, so it prints the unchanged original variable from main.

Therefore, the output is 9, not 81, and not –1.

Question 2 EASY πŸ“Œ Object-Oriented Programming
What will be the output of the following Java code?

public class MathSquare {
public static void main(String[] args) {
int value = 5;
square(value);
System.out.println(value);
}

public static void square(int x) {
x = x * x;
}
}

A. 25
B. 5
C. Compilation error
D. Runtime error
E. The output is undefined

βœ… Correct Answer: B

Java uses pass-by-value, so the method receives only a copy of the variable.
The change to x inside square() does not affect the original value, so it prints 5.

πŸ“– Detailed Explanation:

Java is pass-by-value, meaning the method receives a copy of the variable, not the original.

The variable value is initialized to 5.

The method call square(value) sends a copy of the integer 5 to the method.

Inside square(), the parameter x is a local copy, not the original variable.

The statement x = x * x; changes only this local copy (x becomes 25).

Once the method finishes, the local variable x is discarded.

The original value in main is unchanged.

So System.out.println(value); prints the original value, which is 5.

No errors occur; the program runs normally.

Question 3 EASY πŸ“Œ The this Keyword
What is the purpose of the this keyword in a constructor when initializing instance variables?
A. To call another constructor in the same class
B. To distinguish instance variables from parameters with the same name
C. To call a superclass constructor
D. To make a method static
E. To access local variables

βœ… Correct Answer: B

The this keyword is used in constructors to refer to the instance variables of the current object when parameter names conflict with instance variable names. For example, in this.name = name, this.name refers to the instance variable, while name is the parameter. This disambiguation ensures the correct variable is assigned the value.

πŸ“– Detailed Explanation:

The this keyword is used in constructors to refer to the instance variables of the current object when parameter names conflict with instance variable names. For example, in this.name = name, this.name refers to the instance variable, while name is the parameter. This disambiguation ensures the correct variable is assigned the value.

Question 4 EASY πŸ“Œ Constructor Basics
What happens if you define a constructor with parameters in a class but do not define a no-argument constructor?
A. The compiler provides a default no-argument constructor
B. The class cannot be instantiated
C. A compilation error occurs when creating an object with no arguments
D. The class becomes abstract
E. The constructor is automatically public

βœ… Correct Answer: C

If a class defines a constructor with parameters, the Java compiler does not provide a default no-argument constructor. Attempting to instantiate the class with new ClassName() will result in a compilation error because no matching constructor exists. You must explicitly define a no-argument constructor if you want to allow such instantiation.

πŸ“– Detailed Explanation:

Java automatically provides a no-argument constructor only when the class has zero constructors.

As soon as you define any constructor (even with parameters), Java stops generating the default one.

Example:

class A {
A(int x) {}
}


Here, there is no no-arg constructor.

If you attempt:

A obj = new A();


The compiler searches for a no-argument constructor but does not find one.

Therefore, it throws:

error: constructor A in class A cannot be applied to given types;


So the class can be instantiated, but only using the available constructor, not the no-arg one.

Question 5 MEDIUM πŸ“Œ The this Keyword
Why is the this keyword required in the following setter method?

public void setFirstName(String firstName) {
this.firstName = firstName;
}
A. To call another constructor
B. To distinguish the instance variable from the parameter
C. To make the method static
D. To call the superclass method
E. To prevent recursion

βœ… Correct Answer: B

In the setter method, the parameter firstName has the same name as the instance variable firstName, creating a naming conflict. Using this.firstName refers to the instance variable, while firstName alone refers to the parameter. Without this, the assignment firstName = firstName would assign the parameter to itself, leaving the instance variable unchanged.

πŸ“– Detailed Explanation:

In the setter:

public void setFirstName(String firstName) {
this.firstName = firstName;
}

firstName (right side) β†’ method parameter

this.firstName (left side) β†’ instance variable of the object

Both have the same name, so Java needs this to know that you are referring to the instance field, not the local parameter.

Without this, the assignment: firstName = firstName; would just assign the parameter to itself, doing nothing.

Why other options are incorrect

A. To call another constructor β€” No, that uses this() in constructors.
C. To make the method static β€” this cannot be used in static context.
D. To call the superclass method β€” That uses super, not this.
E. To prevent recursion β€” Not related; recursion depends on method calls, not variable names.

Question 6 MEDIUM πŸ“Œ Constructor Basics
What is the purpose of a private constructor in a class?
A. To allow instantiation from any class
B. To prevent instantiation from outside the class
C. To make the class abstract
D. To allow instantiation only in subclasses
E. To make the constructor static

βœ… Correct Answer: B

A private constructor restricts object creation to within the class itself, preventing external classes from instantiating it. This is commonly used in patterns like Singleton to control instance creation or in utility classes with only static methods. It does not make the class abstract or static, nor does it specifically allow instantiation only in subclasses.

πŸ“– Detailed Explanation:

A private constructor restricts object creation to within the class itself, preventing external classes from instantiating it. This is commonly used in patterns like Singleton to control instance creation or in utility classes with only static methods. It does not make the class abstract or static, nor does it specifically allow instantiation only in subclasses.

Question 7 MEDIUM πŸ“Œ Constructor Basics
What happens if you try to create an instance of a class with a private constructor from outside the class?
A. The code compiles and runs normally
B. A compilation error occurs
C. A runtime exception is thrown
D. The constructor becomes public automatically
E. The instance is created but inaccessible

βœ… Correct Answer: B

A private constructor is only accessible within the same class, so attempting to instantiate the class from outside (e.g., new ClassName()) results in a compilation error. This enforces encapsulation by preventing unauthorized object creation. No runtime exception occurs because the issue is caught at compile time.

πŸ“– Detailed Explanation:

In Java, constructors follow the same access rules as methods.

If a constructor is marked private, only the class itself can instantiate objects of that class.

This is commonly used in singleton patterns, utility classes, or factory methods.

When another class tries to do:

MyClass obj = new MyClass();


Java checks access modifiers.

Since the constructor is private, external code is not allowed to invoke it.

The compiler immediately reports an error:

MyClass() has private access in MyClass


No object is createdβ€”execution never reaches runtime.

Thus, it is always a compile-time failure, not runtime.

Question 8 MEDIUM πŸ“Œ Constructor Basics
Which of the following is true about a default constructor?
A. It is always provided by the compiler
B. It is only provided if no other constructors are defined
C. It has parameters
D. It is always public
E. It returns a value

βœ… Correct Answer: B

The Java compiler automatically provides a no-argument default constructor with an empty body if no constructors are explicitly defined in the class. If any constructor (with or without parameters) is defined, the compiler does not generate a default constructor. The default constructor is package-private by default, not necessarily public, and it does not return a value.

πŸ“– Detailed Explanation:

The Java compiler automatically provides a no-argument default constructor with an empty body if no constructors are explicitly defined in the class. If any constructor (with or without parameters) is defined, the compiler does not generate a default constructor. The default constructor is package-private by default, not necessarily public, and it does not return a value.

Question 9 HARD πŸ“Œ Constructor Basics
What is the output of the following code?

public class Sample {
private static void printNumber(int number) {
System.out.print(number + " ");
}
static {
printNumber(2);
}
static {
printNumber(4);
}
{
printNumber(6);
}
{
printNumber(8);
}
public Sample() {
printNumber(5);
}
public static void main(String[] args) {
new Sample();
}
}
A. 2 4 6 8 5
B. 6 8 5 2 4
C. 2 4 5 6 8
D. 5 6 8 2 4
E. Compilation error

βœ… Correct Answer: A

Static initializers run first when the class is loaded, printing 2 and 4. When a Sample object is created, instance initializers run in order, printing 6 and 8, followed by the constructor, which prints 5. The output is therefore 2 4 6 8 5.

πŸ“– Detailed Explanation:

Static blocks run first, in the order they appear in the class.
The class has two static blocks:
static { printNumber(2); }
static { printNumber(4); }
and 2 instance initializer blocks
{ printNumber(6); }
{ printNumber(8); }

So as soon as the class loads (before main()), the output becomes: 2 4 6 8

Now main() executes
public static void main(String[] args) {
new Sample();
}

A new object is created, so the constructor runs.

public Sample() {
printNumber(5);//This prints:5
}

Combine all outputs in order: From static blocks: 2 4 6 8 , From constructor: 5

Final output: 2 4 6 8 5

Question 10 HARD πŸ“Œ Constructor Basics
Why are instance initializers considered bad practice?
A. They cause compilation errors
B. They make code harder to read and maintain
C. They prevent constructor chaining
D. They are only allowed in abstract classes
E. They cannot initialize fields

βœ… Correct Answer: B

Instance initializers, non-static code blocks, run before constructors but can make code less readable because their purpose is less explicit than constructor logic. Constructors provide a clearer, more maintainable way to initialize objects. They do not cause compilation errors, prevent chaining, or restrict field initialization.

πŸ“– Detailed Explanation:

Instance initializers, non-static code blocks, run before constructors but can make code less readable because their purpose is less explicit than constructor logic. Constructors provide a clearer, more maintainable way to initialize objects. They do not cause compilation errors, prevent chaining, or restrict field initialization.

Want More Practice Questions?

Access all chapter-wise practice questions + 1,624 OCA questions covering all exam objectives

What You'll Get:

β†’ 1,624 practice questions

β†’ 29 full certification tests

β†’ Chapter-wise practice exams

β†’ Detailed explanations for every answer

β†’ Progress tracking & certificates

Start Free Trial Now β†’