Programming In Java | Week 4

Session: JAN-APR 2024

Course name: Programming In Java

Course Link: Click Here

For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 4 Assignment 4 Answers


Q1. Which is the keyword used to specify the default access modifier in java?
a. default
b. DEFAULT
c. package
d. “There is no keyword”

Answer: d. “There is no keyword”


Q2. What is the output of the Java program with access modifiers?
a. FOUR
b. Runtime Error
c. null
d. Compiler Error

Answer: d. Compiler Error


Q3. What is the output of the below Java Code Snippet with access modifiers?
a. Weeks = 0
b. Weeks = 12
c. No Error, blank output
d. Compiler error

Answer: b. Weeks = 12


For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 4 Assignment 4 Answers


Q4. Which of the following is the correct representation of access modifiers in order of increasing visibility?
a. private < default < protected < public
b. private < protected < default < public
c. public < protected < default < private
d. protected < default < private < public

Answer: a. private < default < protected < public


Q5. Which of the following package stores all the standard java classes?
a. java.util
b. java.lang
c. java.java
d. java.packages

Answer: b. java.lang


Q6. Which of the following is/are true about packages in Java?
1. Every class is part of some package.
2. All classes in a file are part of the same package.
3. If no package is specified, the classes in the file go into a special unnamed package.
4. If no package is specified, a new package is created with folder name of class and the class is put in this package.

a. Only 1, 2 and 3
b. Only 1, 2 and 4
c. Only 4
d. Only 1 and 3

Answer: a. Only 1, 2 and 3


For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 4 Assignment 4 Answers


Q7. What is the output of following Java program?
a. Compiler Error
b. Runtime Error
c. Welcome!
d. None of the above

Answer: c. Welcome!


Q8. Which of these access specifiers can be used for an interface?
a. Public
b. Protected
c. private
d. All of the mentioned

Answer: a. Public


Q9. Which of the following is the correct way of implementing an interface salary by class manager?
a. class Java extends NPTEL {}
b. class Java implements NPTEL {}
c. class Java imports NPTEL {}
d. none of the mentioned

Answer: b. class Java implements NPTEL {}


Q10. What will be the output of the following Java program?
a. 0
b. 2
c. 4
d. Compiler Error

Answer: c. 4


For answers or latest updates join our telegram channel: Click here to join

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-assignment-answers


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-assignment-answers/


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.


These are NPTEL Programming In Java Week 4 Assignment 4 Answers


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


These are NPTEL Programming In Java Week 4 Assignment 4 Answers


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


These are NPTEL Programming In Java Week 4 Assignment 4 Answers
The content uploaded on this website is for reference purposes only. Please do it yourself first.
Home
Account
Cart
Search