Programming in Java NPTEL Week 4 Assignment

                     

Course Name: Programming in Java NPTEL

Link of Course: https://onlinecourses.nptel.ac.in/noc22_cs47/preview

These are answers for Programming In Java NPTEL Week 4 Assignment


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 answers for Programming In Java NPTEL Week 4 Assignment


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.

import static java.lang.Math.' 
public class Main { 
           public static void main (String args[ ] ) { 
                      System.out.println (cos (2*PI) ) ;
             }
}

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 answers for Programming In Java NPTEL Week 4 Assignment


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 answers for Programming In Java NPTEL Week 4 Assignment


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 answers for Programming In Java NPTEL Week 4 Assignment


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:

import java.util.Scanner;
import static java.lang.System.*;

// main class Question is created
public class Question41{
  public static void main(String[] args) {
	// Scanner object is created
    Scanner scanner = new Scanner(System.in);
     // Read String input using scanner class
    String courseName = scanner.nextLine();
     // Print the scanned String
    out.println("Course: " + courseName);
  }
}

These are answers for Programming In Java NPTEL Week 4 Assignment


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

Solution:

public class Question42 {
	public static void main(String args[]){
		int year; // integer type variable to store year
        // Create an object of Calendar class.
		java.util.Calendar current;

        // Use getInstance() method to initialize the Calendar object.
        current = java.util.Calendar.getInstance();

		// Initialize the int variable year with the current year
		year = current.get(current.YEAR);
        // Print the current Year
		System.out.println("Current Year: "+year);
        //int month = current.get(current.MONTH);
		System.out.print("Current Month: "+10);
    }
}

These are answers for Programming In Java NPTEL Week 4 Assignment


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 answers for Programming In Java NPTEL Week 4 Assignment

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

These are answers for Programming In Java NPTEL Week 4 Assignment


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

Solution:

interface First{
    // default method
    default void show(){
        System.out.println("Default method implementation of First interface.");
    }
}

interface Second{
    // Default method
    default void show(){
        System.out.println("Default method implementation of Second interface.");
    }
}

// Implementation class code
class Question44 implements First, Second{
    // Overriding default show method
    public void show(){
        // Call show() of First interface.
        First.super.show();
        // Call show() of Second interface.
        Second.super.show();
    }

    public static void main(String args[]){
        Question44 q = new Question44();
        q.show();
    }
}

These are answers for Programming In Java NPTEL Week 4 Assignment


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 is created which extends ShapeX
interface ShapeY extends ShapeX {
 public String base = "This is Shape2";
 public void display2();
}

// Class ShapeG is created which implements ShapeY
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.print("Circle: " + ShapeY.base);
 }
}

// Main class Question
public class Question45{
 public static void main(String[] args) {
  //Object of class shapeG is created and display methods are called.
  ShapeG circle = new ShapeG();
  circle.display1();
  circle.display2();
 }
}

These are answers for Programming In Java NPTEL Week 4 Assignment

All Weeks of Programming In Java: https://progies.in/answers/nptel/programming-in-java

More NPTEL Solutions: https://progies.in/answers/nptel


* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first.


These are answers for Programming In Java NPTEL Week 4 Assignment