Java Practice MCQs – Missing Code Line (OOP Basics)

✅ Question 1

class Student {
    String name;
    int marks;

    void setData(String n, int m) {
        name = n;
        marks = m;
    }

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

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

What should replace // Missing Line?

A) Student.setData(“Rahul”, 85);
B) s1.setData(“Rahul”, 85);
C) setData(“Rahul”, 85);
D) new Student(“Rahul”, 85);


✅ Question 2

class Employee {
    int id;
    double salary;

    void setDetails(int i, double s) {
        id = i;
        salary = s;
    }

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

public class Test {
    public static void main(String[] args) {
        Employee e = new Employee();
        e.setDetails(102, 50000);
        // Missing Line
    }
}

A) show();
B) Employee.show();
C) e.show();
D) new show();


✅ Question 3

class Rectangle {
    int length;
    int width;

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

public class Demo {
    public static void main(String[] args) {
        Rectangle r = new Rectangle();
        r.length = 8;
        r.width = 6;
        // Missing Line
    }
}

A) System.out.println(area());
B) System.out.println(r.area());
C) area(r);
D) Rectangle.area();


✅ Question 4

class Car {
    String brand;

    void setBrand(String b) {
        brand = b;
    }

    void showBrand() {
        System.out.println(brand);
    }
}

public class Run {
    public static void main(String[] args) {
        Car c;
        // Missing Line
        c.setBrand("Honda");
        c.showBrand();
    }
}

A) Car c = new Car();
B) c = new Car();
C) new Car();
D) Car();


✅ Question 5

class Book {
    String title;

    void display() {
        System.out.println(title);
    }
}

public class Library {
    public static void main(String[] args) {
        Book b = new Book();
        // Missing Line
        b.display();
    }
}

A) b.title = “Java Programming”;
B) Book.title = “Java Programming”;
C) title = “Java Programming”;
D) setTitle(“Java Programming”);


✅ Question 6

class Circle {
    double radius;

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

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

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


✅ Question 7

class Person {
    String name;

    void greet() {
        System.out.println("Hello " + name);
    }
}

public class Welcome {
    public static void main(String[] args) {
        Person p = new Person();
        p.name = "Ajay";
        // Missing Line
    }
}

A) greet();
B) Person.greet();
C) p.greet();
D) new greet();


✅ Question 8

class BankAccount {
    double balance;

    void deposit(double amt) {
        balance += amt;
    }

    void showBalance() {
        System.out.println(balance);
    }
}

public class BankTest {
    public static void main(String[] args) {
        BankAccount acc = new BankAccount();
        // Missing Line
        acc.showBalance();
    }
}

A) acc.deposit(2000);
B) deposit(2000);
C) BankAccount.deposit(2000);
D) new deposit(2000);


✅ Question 9

class Laptop {
    String model;

    void setModel(String m) {
        model = m;
    }

    String getModel() {
        return model;
    }
}

public class Shop {
    public static void main(String[] args) {
        Laptop l = new Laptop();
        l.setModel("HP");
        // Missing Line
    }
}

A) System.out.println(getModel());
B) System.out.println(l.getModel());
C) getModel(l);
D) Laptop.getModel();


✅ Question 10

class Temperature {
    double celsius;

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

public class Convert {
    public static void main(String[] args) {
        Temperature t = new Temperature();
        t.celsius = 30;
        // Missing Line
    }
}

A) System.out.println(t.toFahrenheit());
B) System.out.println(toFahrenheit());
C) Temperature.toFahrenheit();
D) toFahrenheit(t);


🔎 Answer Key (Check After Solving)

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

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 *