NPTEL Course: Week 4 Quiz Answers of Java Programming

1. Question: Which access modifier allows a method to be accessible within the same package but not from outside the package?

  • a. default
  • b. private
  • c. public
  • d. protected

Answer: a. default

2. Question: What will happen if you attempt to access a private method from another class in Java?

  • a. The method will be accessible.
  • b. The method will throw an exception.
  • c. The method will be inaccessible.
  • d. The method will be converted to protected.

Answer: c. The method will be inaccessible.

3. Question: What will be the output of the following code?

      class Person {
    int a = 1;
    int b = 0;
    public Person() {
        System.out.println(a + b + " Java ");
    }
}

class Employee extends Person {
    int a = 0;
    int b = 1;
    public Employee() {
        System.out.println(a * b + " Java ");
    }
}

public class Question {
    public static void main(String args[]) {
        Person p = new Employee();
    }
}
    
  • a. 1 Java 10
  • b. 1 Java 0 Java
  • c. 10 Java 0 Java
  • d. 1 Java 1 Java

Answer: b. 1 Java 0 Java

4. Question: Which of the following is a built-in Java package?

  • a. java.util
  • b. my.package.util
  • c. default.package
  • d. system.java

Answer: a. java.util

5. Question: What keyword is used to define a package in Java?

  • a. define
  • b. package
  • c. import
  • d. namespace

Answer: b. package

6. Question: How do you import a specific class from a package in Java?

  • a. import package.*;
  • b. import package.ClassName;
  • c. include package.ClassName;
  • d. use package.ClassName;

Answer: b. import package.ClassName;

7. Question: Consider the following program:

      class Base {
    public void print() {
        System.out.println("Base class...");
    }
}

class Derived extends Base {
    public void print() {
        System.out.println("Derived class...");
    }
}

public class Main {
   private static void main(String[] args) {
        Base b = new Base();
        b.print();
        Derived d = new Derived();
        d.print();
    }
}
    

How many errors does this program contain?

  • a. None
  • b. 1
  • c. 2
  • d. 3

Answer: c. 2

8. Question: Which of the following is true about Java interfaces?

  • a. They cannot contain method implementations.
  • b. An interface can extend multiple classes.
  • c. An interface can only contain abstract methods.
  • d. They allow a class to achieve multiple inheritance.

Answer: d. They allow a class to achieve multiple inheritance.

9. Question: What will happen if you do not specify an access modifier for a class in a package?

  • a. The class will be private by default.
  • b. The class will be protected by default.
  • c. The class will be accessible only within the same package.
  • d. The class will be public by default.

Answer: c. The class will be accessible only within the same package.

10. Question: Which access modifier provides the most restrictive access in Java?

  • a. default
  • b. protected
  • c. private
  • d. public

Answer: c. private

Answers:

  1. a
  2. c
  3. b
  4. a
  5. b
  6. b
  7. c
  8. d
  9. c
  10. c

Leave a Comment

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

Scroll to Top