Programming in Java Nptel Week 4 Assignment Answers

Are you looking for Programming in Java Nptel Week 3 Assignment Answers? You’ve come to the right place! Access the latest and most accurate solutions for your Week 3 assignment in the Programming in Java course

Course Link: Click Here


Programming in Java Nptel Week 4 Assignment Answers
Programming in Java Nptel Week 4 Assignment Answers

Programming in Java Nptel Week 4 Assignment Answers (July-Dec 2024)

1. Which of these access specifiers must be used for main() method?
a) private
b) public
c) protected
d) default

Answer:b) public


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
d) None of the above

Answer:a) 100 marks


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

Answer : c) Encapsulation


4. Consider the 2 programs:

// 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++);
    }
}

a) Both pre-increment and post-increment operators become pre-increment during print.
b) Both pre-increment and post-increment operators become 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.

Answer: c) Both Main1 and Main2 classes give the same output.


5. Which is the least restrictive access modifier in Java?
a) public
b) private
c) protected
d) default

Answer: a) public


6. Choose the correct syntax of a Java Package below.
a) package PACKAGE_NAME;
b) package PACKAGE_NAME.;

c) pkg PACKAGE_NAME;

d) pkg PACKAGE_NAME.;

Answer: a) package PACKAGE_NAME;


7. What is the default package in Java?
a) It is a package that contains all built-in classes.
b) It is a package that needs to be defined as default.
c) It is a package that does not have a name.
d) It is a package used for importing external libraries.

Answer: c) It is a package that does not have a name.


8. A package is a collection of:
a) classes
b) interfaces
c) editing tools
d) classes and interfaces

Answer: d) classes and interfaces


9. In Java, can a subclass in a different package access a superclass’s protected method?
a) Yes, without any restrictions.
b) Yes, but only if they are in the same package.
c) No, protected methods are not accessible by subclasses.
d) No, protected methods are only accessible within the same class.

Answer: a) Yes, without any restrictions.


10. Consider the program given below. What will be the output if the program is executed?

// 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++);
    }
}

a) It will give compile-time error
b) It will give run-time error
c) 1.0
d) 3.14

Answer: c) 1.0


All Weeks of Programming In Java: Click Here

For answers to additional Nptel courses, please refer to this link: NPTEL Assignment Answers


Programming in Java Nptel Week 4 Assignment Answers (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


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

See also  Programming in Java Nptel Week 8 Assignment Answers

Answer: b. Weeks = 12


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

These are Programming in Java Nptel Week 4 Assignment 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 Programming in Java Nptel Week 4 Assignment 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 Programming in Java Nptel Week 4 Assignment Answers

More Weeks of Programming In Java: Click here

More Nptel Courses: https://progiez.com/nptel-assignment-answers


Programming in Java Nptel Week 4 Assignment Answers (July-Dec 2023)

Course Name: Programming In Java

Course Link: Click Here

These are Programming in Java Nptel Week 4 Assignment 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 Programming in Java Nptel Week 4 Assignment 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 Programming in Java Nptel Week 4 Assignment 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 Programming in Java Nptel Week 4 Assignment Answers

More Weeks of Programming In Java: Click here

See also  Programming In Java | Week 4

More Nptel Courses: Click here


Programming in Java Nptel Week 4 Assignment Answers (Jan-Apr 2023)

Course Name: Programming in Java

Course Link: Click Here

These are Programming in Java Nptel Week 4 Assignment 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 Programming in Java Nptel Week 4 Assignment 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 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 Programming in Java Nptel Week 4 Assignment 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 Programming in Java Nptel Week 4 Assignment Answers

More Weeks of Programming In Java: Click Here

More Nptel courses: https://progiez.com/nptel-assignment-answers/


Programming in Java Nptel Week 4 Assignment Answers (July-Dec 2022)

Course Name: Programming in Java NPTEL

See also  Programming in Java | Week 11

Link of Course: Click Here

These are Programming in Java Nptel Week 4 Assignment 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 Programming in Java Nptel Week 4 Assignment 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 Programming in Java Nptel Week 4 Assignment 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


These are Programming in Java Nptel Week 4 Assignment 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.



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 Programming in Java Nptel Week 4 Assignment 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:

Code


Question 2
Complete the code segment to print the current year. Your code should compile successfully.

Solution:

Code

These are Programming in Java Nptel Week 4 Assignment 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:

Code

Question 4
Complete the code segment to call the default method in the interface First and Second.

Solution:

Code

These are Programming in Java Nptel Week 4 Assignment 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 Programming in Java Nptel Week 4 Assignment Answers