Week 04 Assignment Solution
Week 04 Assignment Solution
Week 04 Assignment Solution
PROGRAMMING IN JAVA
Assignment 4
TYPE OF QUESTION: MCQ
Number of questions: 10 Total marks: 10 × 1 = 10
QUESTION 1:
a. private
b. public
c. protected
d. default
Correct Answer:
b. public
Detailed Solution:
main() method must be specified public as it called by Java run time system, outside of the
program. If no access specifier is used then by default member is public within its own package &
cannot be accessed by Java run time system.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 2:
What is the output of the below Java Code Snippet with protected access modifier?
// Teacher.java ------------------
package nptel1;
public class Teacher {
protected void showMarks() {
System.out.println("100 Marks");
}
}
// Student.java ------------------
package nptel2;
import nptel1.*;
public class Student extends Teacher {
void show() {
showMarks();
}
public static void main(String[] args) {
Student st1 = new Student();
st1.show();
}
}
a. 100 marks
b. No output
c. Compiler error
Correct Answer:
a. 100 marks
Detailed Solution:
Through inheritance, one can access a protected variable or method of a class even from outside the
package. Here, we accessed Teacher class of nptel1 from Student class of nptel2.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 3:
What is the process by which we can control what parts of a program can access the members of a
class?
a. Polymorphism
b. Augmentation
c. Encapsulation
d. Recursion
Correct Answer:
c. Encapsulation
Detailed Solution:
Encapsulation in Java is the process by which data (variables) and the code that acts upon them
(methods) are integrated as a single unit. By encapsulating a class's variables, other classes cannot
access them, and only the methods of the class can access them.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 4:
// Main1.java ------------------
public class Main1{
public static void main(String args[]){
int number = 10;
System.out.println(number++ + ++number);
}
}
// Main2.java ------------------
public class Main2{
public static void main(String args[]){
int number = 10;
System.out.println(++number + number++);
}
}
Correct Answer:
Detailed Solution:
The output of both the program are 22. Therefore, option c is correct and we can eliminate option d that
the operators don’t work. Further, the operators are doing exactly what they are supposed to do i.e. pre-
increment first increases the values and post-increment increases the value during the next operation.
The print statement is the next operation; hence it received the post incremented value as well making
option a and b invalid.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
a. public
b. private
c. protected
d. default
Correct Answer:
a. public
Detailed Solution:
A variable or a method marked public is available to all outside classes irrespective of package that a
class lives in. So, public is the least restrictive access modifier in Java.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 6:
a. package PACKAGE_NAME;
b. package PACKAGE_NAME.*;
c. pkg PACKAGE_NAME;
d. pkg PACKAGE_NAME.*;
Correct Answer:
a. package PACKAGE_NAME;
Detailed Solution:
A package declaration statement should end with a package name but not with *.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
Correct Answer:
Detailed Solution:
The default package is a package without a specified name. It’s not recommended to use the default
package for your classes.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 8:
a. classes
b. interfaces
c. editing tools
Correct Answer:
Detailed Solution:
QUESTION 9:
d. No, protected methods are only accessible within the same class.
Correct Answer:
Detailed Solution:
Yes, a subclass in a different package can access a superclass’s protected method, as long as they are in a
subclass relationship.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
Consider the program given below. What will be the output if the program is executed?
c. 1.0
d. 3.14
Correct Answer:
Detailed Solution:
The program gives a compile time error as the Math class is missing.
The static import statement needs to be used to import the static members (e.g., PI) of java.lang.Math.