10 Free Practice Questions for 1Z0-808 Certification
All 10 questions shown below with complete explanations
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.
this Keyword in Constructors and MethodsThe 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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.
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.
Access all chapter-wise practice questions + 1,624 OCA questions covering all exam objectives
β 1,624 practice questions
β 29 full certification tests
β Chapter-wise practice exams
β Detailed explanations for every answer
β Progress tracking & certificates