Programming in Java NPTEL Assignment Answers of Week 4(2023)
1] Which of the following is the correct statement for creating a package?
a] < package name > package;
b] package < package name >;
c] package;
d] < package name >;
2] Which of the following source files cannot be included in a package?
a] classes
b] interfaces
c] enumerations
d] data
3] Which of the following is/are used to access a public package?
a] Refer to the member by its fully qualified name
b] Import the package member
c] Import the member’s entire package
d] Import is not mandatory
4] Which of the following statement(s) is/are false?
a] Java packages are hierarchical.
b] System.out.println() is a predefined java function.
c] Java can have a nested class structure.
d] The Java static keyword is a non-access modifier.
5] Consider the program given below.
a] It will give compile-time error
b] It will give run-time error
c] 1.0
d] 3.14
6] Which of the following is the minimum requirement for executing a Java program?
a] JDK
b] JRE
c] JDK without JRE
d] JRE without JDK
7] Which of the following is required for developing a Java program?
a] JDK
b] JRE
c] JDK without JRE
d] JRE without JDK
8] Which of the following statement(s) is/are correct?
a] Java byte code is machine dependent.
b] Java byte code is generated by the compiler.
c] Java byte code is generated by the interpreter.
d] Java byte code is machine independent.
9] Which of the following is an advantage of methods?
a] Code re-usability
b] Platform independence.
c] Fast execution of codes.
d] Removes compilation error.
10] Consider the following programs:
Choose correct statement about the output of this code segment.
a] Both pre-increment and post-increment operators becomes pre-increment during print.
b] Both pre-increment and post-increment operators becomes post-increment during print.
c] Both Main1 and Main2 classes give the same output.
d] Pre-increment and post-increment operators don’t work during print.
Week 4: Programming Assignments
1] Complete the code segment to execute the following program successfully. You should import the correct package(s) and/or class(s) to complete the code.
Answer :
import java.util.Scanner; import java.io.*; import static java.lang.System.*; import java.util.LinkedList;
2] Complete the code segment to print the current year. Your code should compile successfully.
Answer :
java.util.Calendar current; current = java.util.Calendar.getInstance(); year = current.get(current.YEAR);
3] The program in this assignment is attempted to print the following output:
—————–OUTPUT——————-
This is small
This is medium
This is large
This is extra-large
————————————————-
However, the code is intentionally injected with some bugs. Debug the code to execute the program successfully.
Answer :
interface ExtraLarge{
static String extra = "This is extra-large";
void display();
}
class Large {
public void Print() {
System.out.println("This is small");
}
}
class Medium extends Large {
public void Print() {
super.Print();
System.out.println("This is medium");
}
}
class Small extends Medium {
public void Print() {
super.Print();
System.out.println("This is large");
}
}
4] Complete the code segment to call the default method in the interface Second then First.
Answer :
Second.super.show(); First.super.show();
5] Modify the code segment to print the following output.
————–OUTPUT—————–
Circle: This is Shape1
Circle: This is Shape2
—————————————–
Answer :
// Interface ShapeX is created
interface ShapeX {
public String base = "This is Shape1";
public void display1();
}
interface ShapeY extends ShapeX {
public String base = "This is Shape2";
public void display2();
}
class ShapeG implements ShapeY {
public String base = "This is Shape3";
//Overriding method in ShapeX interface
public void display1() {
System.out.println("Circle: " + ShapeX.base);
}
// Overriding method in ShapeY interface
public void display2() {
System.out.println("Circle: " + ShapeY.base);
}
}