Unit Test – 2: Java Classes, Objects & Constructors
General Instructions:
1. Answer all questions. Write outputs clearly and neatly.
2. Assume all code runs inside a valid main method.
Starter Code:
public class OperatorTest {
public static void main(String[] args) {
// Write your code here for output-based questions.
}
}
3. Do dry run (solve using a pen and paper ) and execute in Java environment to verify.
4. Your brain is your best compiler today — train it well
Q1: What is the output of the following code?
class A {
int x;
A() {
this(5);
System.out.print("A ");
}
A(int x) {
System.out.print("B ");
}
}
class Test {
public static void main(String[] args) {
A obj = new A();
}
}
Q2: Fill in the constructor so the output is Name: John, Age: 25
class Person {
String name;
int age;
// Constructor here
public static void main(String[] args) {
Person p = new Person("John", 25);
System.out.println("Name: " + p.name + ", Age: " + p.age);
}
}
Q3: Will this compile? If not, why?
class Demo {
int x;
Demo(int x) {
this.x = x;
}
}
class Main {
public static void main(String[] args) {
Demo d = new Demo();
}
}
Q4: What will this basic constructor print?
class Hello {
Hello() {
System.out.println("Hello World");
}
public static void main(String[] args) {
new Hello();
}
}
Q5: What is the output when using a default constructor to set a value?
class Number {
int num;
Number() {
num = 100;
}
public static void main(String[] args) {
Number n = new Number();
System.out.println(n.num);
}
}
Q6: What is the output of the inner and outer class names?
class Outer {
String name = "Outer";
class Inner {
String name = "Inner";
void show() {
String name = "Local";
System.out.println(name);
System.out.println(this.name);
System.out.println(Outer.this.name);
}
}
public static void main(String[] args) {
Outer.Inner obj = new Outer().new Inner();
obj.show();
}
}
Q7: What does this constructor chaining output?
class Demo {
int a, b;
Demo(int a) {
this.a = a;
}
Demo(int a, int b) {
this(a);
this.b = b;
}
public static void main(String[] args) {
Demo d = new Demo(3, 4);
System.out.println(d.a + " " + d.b);
}
}
Q8: Will this constructor compile with a return statement? Why or why not?
class Test {
Test() {
System.out.println("Before return");
return;
// System.out.println("After return");
}
public static void main(String[] args) {
new Test();
}
}
Q9: What is the output when counting objects?
class Student {
int id;
Student(int id) {
this.id = id;
System.out.println("Student ID: " + id);
}
public static void main(String[] args) {
Student s1 = new Student(1);
Student s2 = new Student(2);
}
}
Q10: What is the output using an inner class to access outer class data?
class Outer {
int x = 10;
Outer() {
Inner in = new Inner();
in.display();
}
class Inner {
void display() {
System.out.println("x = " + x);
}
}
public static void main(String[] args) {
new Outer();
}
}
Q11: What does this keyword refer to in this constructor?
class Example {
int x;
Example(int x) {
this.x = x;
}
public static void main(String[] args) {
Example e = new Example(10);
System.out.println(e.x);
}
}
Q12: Complete the constructor to make the output Area: 50
class Area {
int length, width;
// Constructor here
public static void main(String[] args) {
Area a = new Area(5, 10);
System.out.println("Area: " + (a.length * a.width));
}
}
Q13: What is the output for this simple constructor?
class Car {
String model;
Car(String model) {
this.model = model;
}
public static void main(String[] args) {
Car c = new Car("Tesla");
System.out.println(c.model);
}
}
Q14: What will be printed when using overloaded constructors?
class Overload {
Overload() {
System.out.println("No args");
}
Overload(int x) {
System.out.println("With arg: " + x);
}
public static void main(String[] args) {
new Overload();
new Overload(5);
}
}
Q15: Explain constructor vs setter in this class.
class Student {
String name;
Student(String name) {
this.name = name;
}
void setName(String name) {
this.name = name;
}
}
Q16: What will this simple constructor print?
class Simple {
Simple() {
System.out.println("Simple constructor");
}
public static void main(String[] args) {
new Simple();
}
}
Q17: Fill this constructor to set a value and print it.
class Value {
int val;
Value(int v) {
val = v;
}
public static void main(String[] args) {
Value v = new Value(42);
System.out.println(v.val);
}
}
Q18: What happens when multiple objects are created?
class Demo {
Demo() {
System.out.println("Constructor called");
}
public static void main(String[] args) {
Demo d1 = new Demo();
Demo d2 = new Demo();
}
}
Q19: How to create an array of objects and initialize them?
class Item {
String name;
Item(String name) {
this.name = name;
}
public static void main(String[] args) {
Item[] items = new Item[3];
items[0] = new Item("Pen");
items[1] = new Item("Book");
items[2] = new Item("Bag");
for (Item i : items) {
System.out.println(i.name);
}
}
}
Q20: What is the output of this empty constructor class?
class Empty {
Empty() {}
public static void main(String[] args) {
System.out.println("Empty constructor");
}
}