Packages and Access Modifiers: past questions

9 questions · 45 marks · 8 papers. Try each one before checking.

Solve them and check answers →Read the notes first

2023 Aug

Question 14 MSQ · 3 marks

Consider the three Java programs given below.

A.java:

package pack1;
import java.util.List;
public class A {
    private void getMessage() { //METHOD-1
        System.out.println("getMessage with private");
    }
    public void getMessage(int x) { //METHOD-2
        System.out.println("getMessage with public");
    }
    void getMessage(String y) { //METHOD-3
        System.out.println("getMessage with default");
    }
    protected void getMessage(List<String> z) { //METHOD-4
        System.out.println("getMessage with protected");
    }
}

B.java:

package pack1;
public class B {
}

C.java:

package pack2;
import pack1.A;
public class C extends A{
}

Choose the correct option with respect to METHODs 1, 2, 3, and 4 inside class A.

  1. Class B can access METHODs 1, 2 and 4

  2. Class C can access METHODs 2, 3 and 4

  3. Class B and C both can access all the four methods.

  4. Class B can access METHODs 2, 3 and 4

  5. Class C can access METHODs 2, 4

2023 Dec

Question 12 MCQ · 4 marks

Consider two Java files located in two different packages as shown below.

//MathUtility.java
package mathutil;
public class MathUtility{
    long getFibonacci(int n){
        // return Fibonacci(n)
    }
    protected long add(long n1, long n2){
        // return n1 + n2
    }
}
//Test1.java
package test;
import mathutil.*;
class Calculator extends MathUtility{
    long FibSum(int num, int i) {
        long fib = this.getFibonacci(i); // LINE 1
        long sum = this.add(num, fib);   // LINE 2
        return sum;
    }
}
class Test1{
    public static void main(String args[]) {
        Calculator c = new Calculator();
        long val = c.add(3, 4);          // LINE 3
        System.out.println("Fibonacci Sum: " + c.FibSum(4, 2));
    }
}

Choose the correct option regarding these two .java files.

  1. LINE 1 & LINE 3 will lead to compilation error.

  2. LINE 2 & LINE 3 will lead to compilation error.

  3. Only LINE 1 will lead to compilation error.

  4. The code will compile successfully.

2024 Mar 24

Question 3 MCQ · 6 marks

Consider two Java files located in two different packages as shown below.

A.java:

package com.pack1;
public class A {
    void methodOne() {
        System.out.println("Display methodOne");
    }
    private void methodTwo() {
        System.out.println("Display methodTwo");
    }
    protected void methodThree() {
        System.out.println("Display methodThree");
    }
    public void methodFour() {
        System.out.println("Display methodFour");
    }
}

B.java:

package com.pack2;
import com.pack1.A;
public class B extends A {
    public static void main(String[] args) {
        B obj = new B();
        obj.methodOne();   //LINE 1
        obj.methodTwo();   //LINE 2
        obj.methodThree(); //LINE 3
        obj.methodFour();  //LINE 4
    }
}

Choose the correct option.

  1. LINE 2 and LINE 3 generate compilation errors.

  2. LINE 1, LINE 2, and LINE 3 generate compilation errors.

  3. LINE 1 and LINE 2 generate compilation errors.

  4. Only LINE 2 generates a compilation error.

2024 Aug 04

Question 3 MCQ · 6 marks

Consider two Java files located in two different packages as shown below.

Vehicle.java:

package com.transport;
public class Vehicle {
    void startEngine() {
        System.out.println("Engine started");
    }
    private void stopEngine() {
        System.out.println("Engine stopped");
    }
    protected void accelerate() {
        System.out.println("Accelerating");
    }
    public void brake() {
        System.out.println("Braking");
    }
}

Car.java:

package com.automobile;
import com.transport.Vehicle;
public class Car extends Vehicle {
    public static void main(String[] args) {
        Car car = new Car();
        car.startEngine(); // LINE A
        car.stopEngine();  // LINE B
        car.accelerate();  // LINE C
        car.brake();       // LINE D
    }
}

Choose the correct option.

  1. LINE A and LINE C generate compilation errors.

  2. LINE B and LINE C generate compilation errors.

  3. LINE A and LINE B generate compilation errors.

  4. Only LINE B generates a compilation error.

2025 Mar 16

Question 9 MCQ · 7 marks

Consider two Java files located in different packages as shown below.

A.java:

package pack1;
public class A {
    void AMethod() {
        System.out.println("Method A");
    }
    private void BMethod() {
        System.out.println("Method B");
    }
    protected void CMethod() {
        System.out.println("Method C");
    }
    public void DMethod() {
        System.out.println("Method D");
    }
}

B.java:

package pack2;
import pack1.A;
public class B extends A {
    public void testMethods() {
        super.AMethod(); // LINE 1
        super.BMethod(); // LINE 2
        super.CMethod(); // LINE 3
        super.DMethod(); // LINE 4
    }
}

Choose the correct option.

  1. Only LINE 2 generates compilation error.

  2. LINE 2 and LINE 3 generate compilation errors.

  3. LINE 1, LINE 2 and LINE 3 generate compilation errors.

  4. LINE 1 and LINE 2 generate compilation errors.

2026 Apr 06

Question 3 MCQ · 6 marks

Consider the two Java files given below.

Word.java:

package linguistics;
public class Word {
    private String text;
    private String language;
    private int syllables;
    // Constructor to initialize instance variables
    public String getText(String text) {
        return text;
    }
    protected String getLanguage(String language) {
        return language;
    }
    private int getSyllables(int syllables) {
        return syllables;
    }
    public int showSyllables(int syllables) {
        return getSyllables(syllables);
    }
}

LinguistTest.java:

package analysis;
import linguistics.Word;
public class LinguistTest {
    public static void main(String[] args) {
        Word w = new Word();
        System.out.println("Syllables: " + w.showSyllables(3)); // LINE 1
        System.out.println(w.getLanguage("English"));           // LINE 2
        System.out.println(w.getText("Hello"));                 // LINE 3
    }
}

Choose the correct option.

  1. LINE 1 and LINE 2 generate compilation errors.

  2. Only LINE 2 generates compilation error.

  3. LINE 2 and LINE 3 generate compilation errors.

  4. No compilation errors.

2025 Aug 03

Question 5 MCQ · 6 marks

You are developing a logistics system. The following files are part of a Java application.

File: TransportUnit.java

package logistics.base;
public class TransportUnit {
    public String getId() {
        return "GenericUnit-001";
    }
    protected double calculateFuelCost(double distance, double rate) {
        return distance * rate;
    }
    private String getInternalCode() {
        return "INT-LOG-999";
    }
}

File: DeliveryTruck.java

package logistics.vehicle;
import logistics.base.TransportUnit;
public class DeliveryTruck extends TransportUnit {
    public String getId() {
        return "Truck-Delivery-204";
    }
    protected double calculateFuelCost(double distance, double rate) {
        return (distance * rate) + 20.0; // Includes loading cost
    }
    public String getInternalCode() {
        return "TRK-INT-204";
    }
}

File: FleetManager.java

package logistics.app;
import logistics.base.TransportUnit;
import logistics.vehicle.DeliveryTruck;
public class FleetManager {
    public static void main(String[] args) {
        TransportUnit unit = new DeliveryTruck();
        System.out.println(unit.getId());                      // LINE 1
        System.out.println(unit.calculateFuelCost(100, 1.5));  // LINE 2
        System.out.println(unit.getInternalCode());            // LINE 3
    }
}

Choose the correct option.

  1. The program compiles and prints:

    Truck-Delivery-204
    170.0
    TRK-INT-204
  2. Only LINE 1 causes a compilation error.

  3. Only LINE 3 causes a compilation error.

  4. LINE 2 and LINE 3 both cause compiler errors.

2022 July

Question 10 MCQ · 3 marks

Consider the Java code given below.

A.java:

package com.pack1;
public class A {
    private void getShow() { //METHOD-1
        System.out.println("Show with no param");
    }
    public void getShow(int x) { //METHOD-2
        System.out.println("Show with int param");
    }
    void getShow(String y) { //METHOD-3
        System.out.println("Show with string param");
    }
    protected void getShow(Object z) { //METHOD-4
        System.out.println("Show with Object param");
    }
}

B.java:

package com.pack1;
public class B {
}

C.java:

package com.pack2;
import com.pack1.A;
public class C extends A{
}

Choose the correct option with respect to METHODs 1, 2, 3 and 4 inside class A.

  1. Class B can access METHODs 1, 2 and 4; Class C can access METHODs 1, 2 and 4

  2. Class B can access METHODs 2, 3 and 4; Class C can access METHODs 2, 4

  3. Class B can access METHODs 1, 2, 3 and 4; Class C can access METHODs 1, 2 and 4

  4. Class B and C both can access all the four methods.

Question 16 MSQ · 4 marks

Consider the three Java programs given below.

Student.java:

package college;
public class Student {
    private String name, college;
    public Student(String name, String college) {
        this.name = name; this.college = college;
    }
    public String getName() {
        return name;
    }
    public String toString() {
        return name + " " + college;
    }
}

Student.java:

package dance;
public class Student {
    String name, type;
    public Student(String name, String type) {
        this.name = name; this.type = type;
    }
    public String getType() {
        return type;
    }
    public String toString() {
        return name + " " + type;
    }
}

Test.java:

package pack3;
public class Test {
    public static void main(String[] args) {
        ******CODE BLOCK******
        System.out.println(obj1);
        System.out.println(obj2);
        System.out.println(obj3);
    }
}

Choose the correct option(s) to fill in the CODE BLOCK so that the output is:

Sam IITM
Sam Traditional
Ram Traditional
  1. var obj1 = new college.Student("Sam", "IITM");
    var obj2 = new dance.Student(obj1.getName(), "Traditional");
    var obj3 = new dance.Student("Ram", obj2.getType());
  2. var obj1 = new Student("Sam", "IITM");
    var obj2 = new Student(obj1.getName(), "Traditional");
    var obj3 = new Student("Ram", obj2.getType());
  3. college.Student obj1 = new college.Student("Sam", "IITM");
    dance.Student obj2 = new dance.Student(obj1.getName(), "Traditional");
    dance.Student obj3 = new dance.Student("Ram", obj2.getType());
  4. Student obj1 = new college.Student("Sam", "IITM");
    Student obj2 = new dance.Student(obj1.getName(), "Traditional");
    Student obj3 = new dance.Student("Ram", obj2.getType());

Check your answers →

Weeks covering this topic

Other Java topics