Java Practice MCQs – Missing Code (Constructors)

✅ Question 1

class Student {
    String name;
    int age;

    Student(String n, int a) {
        name = n;
        age = a;
    }

    void display() {
        System.out.println(name + " " + age);
    }
}

public class Main {
    public static void main(String[] args) {
        // Missing Line
        s1.display();
    }
}

A) Student s1 = new Student();
B) Student s1 = new Student(“Rahul”, 18);
C) Student(“Rahul”, 18);
D) new Student(“Rahul”, 18);


✅ Question 2

class Employee {
    int id;

    Employee() {
        id = 100;
    }

    void show() {
        System.out.println(id);
    }
}

public class Test {
    public static void main(String[] args) {
        // Missing Line
        e.show();
    }
}

A) Employee e = new Employee();
B) Employee();
C) new Employee();
D) Employee e;


✅ Question 3

class Book {
    String title;

    Book(String t) {
        title = t;
    }
}

public class Library {
    public static void main(String[] args) {
        Book b;
        // Missing Line
        System.out.println(b.title);
    }
}

A) b = new Book();
B) b = new Book(“Java”);
C) Book(“Java”);
D) new Book(“Java”);


✅ Question 4

class Rectangle {
    int length;
    int width;

    Rectangle(int l, int w) {
        length = l;
        width = w;
    }

    int area() {
        return length * width;
    }
}

public class Demo {
    public static void main(String[] args) {
        // Missing Line
        System.out.println(r.area());
    }
}

A) Rectangle r = new Rectangle();
B) Rectangle r = new Rectangle(5, 4);
C) new Rectangle(5, 4);
D) Rectangle(5, 4);


✅ Question 5

class Car {
    String brand;

    Car() {
        brand = "Toyota";
    }
}

public class Run {
    public static void main(String[] args) {
        Car c = new Car();
        // Missing Line
    }
}

A) System.out.println(brand);
B) System.out.println(c.brand);
C) System.out.println(Car.brand);
D) print(c.brand);


✅ Question 6

class Circle {
    double radius;

    Circle(double r) {
        radius = r;
    }

    double area() {
        return 3.14 * radius * radius;
    }
}

public class TestCircle {
    public static void main(String[] args) {
        Circle c1 = new Circle(7);
        // Missing Line
    }
}

A) System.out.println(area());
B) System.out.println(c1.area());
C) Circle.area();
D) area(c1);


✅ Question 7

class Laptop {
    String model;
    int price;

    Laptop(String m, int p) {
        model = m;
        price = p;
    }
}

public class Shop {
    public static void main(String[] args) {
        // Missing Line
        System.out.println(l.model + " " + l.price);
    }
}

A) Laptop l = new Laptop();
B) Laptop l = new Laptop(“Dell”, 60000);
C) Laptop(“Dell”, 60000);
D) new Laptop(“Dell”, 60000);


✅ Question 8

class BankAccount {
    double balance;

    BankAccount() {
        balance = 5000;
    }

    BankAccount(double b) {
        balance = b;
    }
}

public class BankTest {
    public static void main(String[] args) {
        // Missing Line
        System.out.println(a.balance);
    }
}

(You want balance = 10000)

A) BankAccount a = new BankAccount();
B) BankAccount a = new BankAccount(10000);
C) BankAccount(10000);
D) new BankAccount(10000);


✅ Question 9

class Person {
    String name;

    Person() {
        name = "Guest";
    }
}

public class Welcome {
    public static void main(String[] args) {
        Person p;
        // Missing Line
        System.out.println(p.name);
    }
}

A) p = new Person();
B) new Person();
C) Person();
D) Person p = Person();


✅ Question 10

class Temperature {
    double celsius;

    Temperature(double c) {
        celsius = c;
    }

    double toFahrenheit() {
        return (celsius * 9/5) + 32;
    }
}

public class Convert {
    public static void main(String[] args) {
        // Missing Line
        System.out.println(t.toFahrenheit());
    }
}

A) Temperature t = new Temperature();
B) Temperature t = new Temperature(25);
C) new Temperature(25);
D) Temperature(25);


🔎 Answer Key (Check After Solving)

  1. B
  2. A
  3. B
  4. B
  5. B
  6. B
  7. B
  8. B
  9. A
  10. B

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *