Programming In Java | Week 4
Programming In Java Week 4 Answers NPTEL
Quiz
Q1. 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 >;
Q2. Which of the following source files cannot be included in a package?
a. classes
b. interfaces
c. enumerations
d. data
Q3. 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
Q4. 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.
Q5. Consider the program given below.
What will be the output if the above program is executed?
a. It will give compile-time error
b. It will give run-time error
c. 1.0
d. 3.14
Q6. 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
Q7. Which of the following is required for developing a Java program?
a. JDK
b. JRE
c. JDK without JRE
d. JRE without JDK
Q8. 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.
Q9. 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.
Q10. 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 Mainl and Main2 classes give the same output.
d. Pre-increment and post-increment operators don’t work during print.
Assignment
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.
Solution:
import java.util.Scanner;
import java.util.LinkedList;
2. Complete the code segment to print the current year. Your code should compile successfully.
Solution:
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.
Solution:
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.
Solution:
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
————————————————-
Solution:
// 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);
}
}
* The material and content uploaded on this website are for general information and reference purposes only and don’t copy the answers of this website to any other domain without any permission or else copyright abuse will be in action.
Please do it by your own first!
