Programming In Java | Week 4
Session: JULY-DEC 2023
Course Name: Programming In Java
Course Link: Click Here
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Programming Assignment
Question 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 static java.lang.System.*;
Question 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);
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Question 3
The program in this assignment is attempted to print the following output:
—————–OUTPUT——————-
This is large
This is medium
This is small
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 large");
}
}
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 small");
}
}
class Question43 implements ExtraLarge{
public static void main(String[] args) {
Small s = new Small();
s.Print();
Question43 q = new Question43();
q.display();
}
public void display(){
System.out.print(extra);
}
}
Question 4
Complete the code segment to call the default method in the interface First and Second.
Solution:
First.super.show();
Second.super.show();
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Question 5
Modify the code segment to print the following output.
—————–OUTPUT——————-
Circle: This is Shape1
Circle: This is Shape2
————————————————-
Solution:
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";
public void display1() {
System.out.println("Circle: " + ShapeX.base);
}
public void display2() {
System.out.print("Circle: " + ShapeY.base);
}
}
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
More Weeks of Programming In Java: Click here
More Nptel Courses: Click here
Session: JAN-APR 2023
Course Name: Programming in Java
Course Link: Click Here
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
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 >;
Answer: b. package < package name >;
Q2. Which of the following source files cannot be included in a package?
a. classes
b. interfaces
c. enumerations
d. data
Answer: 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
Answer: a, b, c
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.
Answer: a. Java packages are hierarchical.
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
Answer: a. It will give compile-time error
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
Answer: b, d
Q7. Which of the following is required for developing a Java program?
a. JDK
b. JRE
c. JDK without JRE
d. JRE without JDK
Answer: a. 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.
Answer: b, d
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.
Answer: a. Code re-usability
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.
Answer: c. Both Mainl and Main2 classes give the same output.
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Programming In Java Programming Assignment
Question 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;
Question 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);
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Question 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");
}
}
Question 4
Complete the code segment to call the default method in the interface Second then First.
Solution:
Second.super.show();
First.super.show();
Question 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);
}
}
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
More Weeks of Programming In Java: Click Here
More Nptel courses: https://progiez.com/nptel/
Session: JULY-DEC 2022
Course Name: Programming in Java NPTEL
Link of Course: Click Here
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Q1. Which of these is a mechanism for naming and visibility control of a class and its content?
a. Objects.
b. Intertaces.
c. Packages.
d. Both a and b
Answer: c. Packages.
Q2. Which of the following is false statement about packages in java?
a. Packages are used to organize a set of related classes and interfaces.
b. Packages are used for preventing naming conflicts.
c. Packages provide code reusabilty.
d. Packages cannot be considered as data encapsulation.
Answer: d. Packages cannot be considered as data encapsulation.
Q3. Which of the following is/are interface(s) of java.awt package?
a. CardLayout
b. Checkbox
c. Choice
d. MeuContainer
Answer: d. MeuContainer
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Q4. Which of the following statement(s) is/are false?
a. The default package in the Java language is java.lang.
b. String is a final class and it is present in java.lang package.
c. Cloneable is a class present in java.lang package.
d. Thread is a class present in java.lang package.
Answer: c. Cloneable is a class present in java.lang package.
Q5. Consider the program given below.
//Code
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
Answer: c. 1.0
Q6. Which of the following packages is used to includes utility classes like Calendar, Collections, Date?
a. java.lang
b. java.util
c. java.net
d. java.awt
Answer: b. java.util
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Q7. Which of the following statement(s) is/are false?
a. Comparable interface is present in java.lang package
b. Iterator interface is present in java.util package.
c. Compare() is a method used in Comparable interface.
d. Serializable interface is present in java.io package.
Answer: c. Compare() is a method used in Comparable interface.
Q8. Image size is too small for OCR Engine 2. Please use Engine 1.
a. public interface Question { void method(int value){ System.out.println(“Nptel”); }
b. public interface Question { void method(int value){}
c. public interface Question { }
d. public interface Question { default void method(int value){ System.out.println(“Nptel”);}
Answer: b, c, d
Q9. Which of the following is false statement about interface in java?
a. An interface can extend other interfaces.
b. The interface body can contain abstract methods, default methods, and static methods.
c. All constant values defined in an interface are implicitly public, static, and final.
d. A static method in an interface are implicitly private.
Answer: d. A static method in an interface are implicitly private.
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Q10. Which of these access specifier(s) can be used for an interface?
a. Public
b. Protected
c. Private
d. Both b and c
Answer: a. Public
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Programming Assignment Solutions
Question 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:
Code
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Question 2
Complete the code segment to print the current year. Your code should compile successfully.
Solution:
Code
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Question 3
The program in this assignment is attempted to print the following output:
OUTPUT
This is large
This is medium
This is small
This is extra-large
However, the code is intentionally injected with some bugs. Debug the code to execute the program successfully.
Solution:
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Code
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Question 4
Complete the code segment to call the default method in the interface First and Second.
Solution:
Code
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
Question 5
Modify the code segment to print the following output.
OUTPUT
Circle: This is Shape1
Circle: This is Shape2
Solution:
Code
These are NPTEL Programming In Java Week 4 Assignment 4 Answers
More NPTEL Solutions: https://progiez.com/nptel

This content is uploaded for study, general information, and reference purpose only.