Programming in Java NPTEL Assignment Answers of Week 3 (2023)

In this article, you will get NPTEL Assignment Answers of Week 3 (2023) of the course Programming in Java


1. Which of the following statement(s) is/are correct about the constructor?
a. Constructors cannot be synchronized in Java.
b. Java does not provide a default copy constructor.
c. A constructor cannot be overloaded.
d. “this” or “super” can be used in a constructor.

Answer: a, b, d


2. Which of the following statement(s) is/are true?
a. You can write a new instance method in the subclass with the same signature as the one in the superclass, thus overriding it.
b. You can write a new static method in the subclass with the same signature as the one in the superclass, thus hiding it.
c. A subclass inherits all of its parent’s public and protected members, no matter what package the subclass is in.
d. You cannot declare new methods in the subclass that are not in the superclass.

Answer: a, b, c


3. Consider the following piece of code.

class Test {
       void exam() { }
}
public class Test 1 extends Test {
    _________void exam() { }      // insert correct keyword
    public static void main (String [ ] args) {
        system.out.printIn("Correct") ;
    }
}

Fill in the blank with the appropriate keyword(s) from the list given below so that the program compiles successfully.
a. abstract
b. final
c. default
d. public

Answer: b. final and d. public


4. How many instances of abstract class can be created?
a. 0
b. 1
c. 2
d. Multiple

Answer: a. 0


5. Structuring a Java class such that only methods within the class can access its instance variables is referred to as ______.
a. object orientation
b. inheritance
c. platform independence
d. encapsulation

Answer: d. encapsulation


6. Which of the following statement(s) is/are true?
a. A final method cannot be overridden in a subclass.
b. The advantage of private static methods is that they can be reused later if you need to reinitialize the class variable.
c. Class methods cannot use this keyword as there is no instance for this to refer to.
d. A final method can be overridden in a subclass.

Answer: a, b, c


7. Consider the following piece of code.

public class Question {
       Question ( ) {
         this ( ) ;
    }
public static void main (String [largs)
       Question obj = new Question ( ) ;
         System.out.println ("Java") ;
    }
}

Which of the following is the output of the above program?
a. Java
b. There will be a compile-time error.
c. JavaJava.
d. The program will give a runtime error.

Answer: b. There will be a compile-time error.


8. Consider the following program.

public class Question
{
       public static void main (String [] args) {
          string str = "Programming in java.";
               System.out.printIn(str.charAt (4) +str.substring (8,11));
       }
}

What is the output of the above program?
a. java
b. ring
c. r min
d. gram

Answer: b. ring


9. Which of the following statement(s) is/are False?
a. Hiding internal data from the outside world and accessing it only through publicly exposed methods is known as data encapsulation.
b. Common behavior can be defined in a superclass and inherited into a subclass using the extends keyword.
c. The term “class variable” is another name for a non-static field.
d. A local variable stores a temporary state; it is declared inside a method.

Answer: c. The term “class variable” is another name for a non-static field.


10. Which of the following statement(s) is/are true?
a. Static methods in interfaces are never inherited.
b. You will get a compile-time error if you attempt to change an instance method in the superclass to a static method in the subclass.
c. You can prevent a class from being subclassed by using the final keyword in the class’s declaration.
d. An abstract class can only be subclassed; it cannot be instantiated.

Answer: a, b, c, d


Programming Assignment Answers


Week 3 : Programming Assignment 1

Define a class Point with two fields x and y each of type double. Also, define a method distance(Point p1, Point p2) to calculate the distance between points p1 and p2 and return the value in double. Complete the code segment given below. Use Math.sqrt( ) to calculate the square root.

Answer:

class Point{
  double x;
  double y;

public static void distance(Point p1,Point p2){
        double d;
	  d=Math.sqrt((p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y));
	  System.out.println(d);
  }	
}

Week 3 : Programming Assignment 2

This program to exercise the call of static and non-static methods. A partial code is given defining two methods, namely sum( ) and multiply ( ). You have to call these methods to find the sum and product of two numbers. Complete the code segment as instructed. 

Answer:

QuestionScope st = new QuestionScope(); // Create an object to call non-  
                                                //static method 
      int result1=st.sum(n1,n2); // Call the method
      int result2=QuestionScope.multiply(n1,n2);	// Create an object to call 
                                                //static method 
		
		System.out.println(result1);
		System.out.println(result2);

Week 3 : Programming Assignment 3

Complete the code segment to swap two numbers using call by object reference.

Answer:

public static void swap(Question t) {
int temp = t.e1;
    t.e1 = t.e2;
    t.e2 = temp;
  }

Week 3 : Programming Assignment 4

This program is related to the generation of Fibonacci numbers.>

For example: 0,1, 1,2, 3,5, 8, 13,… is a Fibonacci sequence where 13 is the 8th Fibonacci number.

A partial code is given and you have to complete the code as per the instruction given .

Answer:

if (n==1)     
            return 0;
        else if(n==2)
            return 1; 			
return fib(n - 1) + fib(n - 2);

Week 3 : Programming Assignment 5

A class Shape is defined with two overloading constructors in it. Another class Test1 is partially defined which inherits the class Shape. The class Test1 should include two overloading constructors as appropriate for some object instantiation shown in main() method. You should define the constructors using the super class constructors. Also, override the method calculate( ) in Test1 to calculate the volume of a Shape.

Answer:

//Template code:
	double height;
	Test1(double length,double h) {
//base class constructor with one parameter is called
		super(length);
		height=h;
	}
	
	Test1(double length,double breadth,double h) {
//base class constructor having two argument is called
		super(length,breadth);
		height=h;
	}

	double calculate()	{
		return length*breadth*height;
	}



2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *