Java MCQ

Previous Java Multiple Choice Questions Next

Java Tricky Packages Questions

1

What happens when you compile this?
// File: com/test/MyClass.java
package com.test;
class MyClass {
  public static void main(String[] args) {
    System.out.println("Hello");
  }
}

Correct Answer: B) Compilation Error - file must be in com/test directory

Java requires that the directory structure matches the package declaration. The file must be in a directory structure: com/test/MyClass.java

2

What is the output?
// File A.java
package p1;
public class A {
  protected void show() {
    System.out.println("A");
  }
}

// File B.java
package p2;
import p1.A;
class B extends A {
  public static void main(String[] args) {
    A obj = new A();
    obj.show();
  }
}

Correct Answer: B) Compilation Error

Protected methods are accessible in subclasses through inheritance, but cannot be accessed using parent class reference from different package. Use 'new B().show()' instead.

3

Which import statement is valid?
package com.company.util;
// Import statements here

Correct Answer: B) import com.company.util.*;

Wildcard imports can only import all classes from a specific package, not subpackages. Option A imports com.company but not com.company.util, Option C has invalid syntax.

4

What happens when compiling?
// File: DefaultClass.java
class DefaultClass {
  void display() {
    System.out.println("Default");
  }
}

// File: Test.java (different package)
import DefaultClass;
public class Test {
  public static void main(String[] args) {
    new DefaultClass().display();
  }
}

Correct Answer: B) Compilation Error - cannot import default class

Default (package-private) classes are only accessible within their own package. They cannot be imported or accessed from different packages.

5

What is the result?
// p1/ClassA.java
package p1;
public class ClassA {
  public ClassA() {
    System.out.print("A");
  }
}

// p2/ClassB.java
package p2;
import p1.ClassA;
public class ClassB {
  public static void main(String[] args) {
    new ClassA();
  }
}

Correct Answer: A) A

Public classes can be accessed from any package. The import statement and public access modifier allow ClassB to instantiate ClassA from a different package.

6

What happens during compilation?
// File: util/Logger.java
package util;
public class Logger {
  static void log(String msg) {
    System.out.println(msg);
  }
}

// File: app/Main.java
package app;
public class Main {
  public static void main(String[] args) {
    Logger.log("Hello");
  }
}

Correct Answer: B) Compilation Error - method must be public

The log() method has package-private (default) access. It cannot be accessed from a different package (app), even though the Logger class is public.

7

Which statement about packages is TRUE?

Correct Answer: A) Package declaration must be first statement

Package declaration (if present) must be the first non-comment line in a Java file. Only one public class per file, not per package. Import statements come after package declaration.

8

What is the output?
// pkg/Base.java
package pkg;
public class Base {
  int defaultVar = 1;
  protected int protectedVar = 2;
}

// pkg/Derived.java
package pkg;
public class Derived extends Base {
  void access() {
    Base b = new Base();
    System.out.print(b.defaultVar + " " + b.protectedVar);
  }
}

Correct Answer: A) 1 2

Within the same package, both default (package-private) and protected members are accessible, even through object reference of parent class.

9

What happens?
// File: MyClass.java
public class MyClass {
  public static void main(String[] args) {
    java.util.Date date = new java.util.Date();
    System.out.println(date);
  }
}

Correct Answer: A) Prints current date

Using fully qualified class names (java.util.Date) eliminates the need for import statements. This is called explicit package import.

10

Which access modifier allows access from different package ONLY through inheritance?

Correct Answer: B) protected

Protected members are accessible within the same package (like default) AND in subclasses in different packages through inheritance only.

Previous Java Multiple Choice Questions Next