Assignment Solution 4
Assignment Solution 4
PROGRAMMING IN JAVA
Assignment 4
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
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
Correct Answer:
a. default
Detailed Solution:
The default access modifier (no explicit modifier specified) allows access to the method within the same
package but not from outside. It is also known as "package-private."
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
What will happen if you attempt to access a private method from another class in Java?
Correct Answer:
Detailed Solution:
A private method is accessible only within the same class. Any attempt to access it from another class
results in a compilation error.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
class Person {
int a = 1;
int b = 0;
public Person() {
System.out.println(a + b + " Java " );
}
}
a. 1 Java 10
0 Java 0
b. 1 Java
0 Java
c. 10 Java
0 Java
d. 1 java 1
0 Java 0
Correct Answer:
b. 1 Java
0 Java
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
If no super() or this() is included explicitly within the derived class constructor, the super() is implicitly
invoked by the compiler. Therefore, in this case, the Person class constructor is called first and then the
Employee class constructor is called. Up casting is allowed.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
a. java.util
b. my.package.util
c. default.package
d. system.java
Correct Answer:
a. java.util
Detailed Solution:
`java.util` is a built-in Java package containing utility classes such as `ArrayList`, `HashMap`, and
`Scanner`.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
a. define
b. package
c. import
d. namespace
Correct Answer:
b. package
Detailed Solution:
The `package` keyword is used in Java to define a package. For example, `package mypackage;` defines a
package named `mypackage`.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
a. import package.*;
b. import package.ClassName;
c. include package.ClassName;
d. use package.ClassName;
Correct Answer:
b. import package.ClassName;
Detailed Solution:
The syntax `import package.ClassName;` imports a specific class from a package. For example, `import
java.util.Scanner;` imports the `Scanner` class from the `java.util` package.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
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
Correct Answer:
b. 1
Detailed Solution:
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
Correct Answer:
d. They allow a class to achieve multiple inheritance.
Detailed Solution:
Interfaces in Java provide a mechanism to achieve multiple inheritance because a class can implement
multiple interfaces.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
What will happen if you do not specify an access modifier for a class in a package?
Correct Answer:
c. The class will be accessible only within the same package.
Detailed Solution:
If no access modifier is specified for a class, it gets the default access level, making it accessible only
within the same package.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
a. default
b. protected
c. private
d. public
Correct Answer:
c. private
Detailed Solution:
The `private` modifier provides the most restrictive access, allowing access only within the class where it
is defined.