Programming in Java Nptel Week 3 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


These are Programming in Java Nptel Week 3 Assignment Answers
Programming in Java Nptel Week 3 Assignment Answers

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


Q1.What will be the output of the following program?

a. Static Method
b. Throws a NullPointerException
c. Compile-time error
d. Run time error

Answer: a. Static Method


Q2.What will be the output of the below program.

a. value of a = 20
b. error: cannot assign a value to final variable ‘a’
c. error: unknown variable ‘a’ in class subDemoClass
d. value of a = 40

Answer: b. error: cannot assign a value to final variable ‘a’


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

These are Programming in Java Nptel Week 3 Assignment Answers


Q3.All the variables of interface should be?
a. default and final
b. default and static
c. public, static and final
d. protect, static and final

Answer: c. public, static and final


Q4.What will be the output of the below program

a. 7 7.4
b. 6 6.4
c. 7 9
d. 9 7

Answer: c. 7 9


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

These are Programming in Java Nptel Week 3 Assignment Answers


Q5.What will be the output of the following Java code?

a. 2 3
b. 3 3
c. Runtime Error
d. Compilation Error

Answer: b. 3 3


Q6. If a variable of primitive datatype in Java is declared as final, then
a. It cannot get inherited
b. Its value cannot be changed
c. It cannot be accessed in the subclass
d. All of the above

Answer: b. Its value cannot be changed


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

These are Programming in Java Nptel Week 3 Assignment Answers


Q7. Members which are not intended to be inherited are declared as
a. Public members
b. Protected members
c. Private members
d. Private or Protected members

Answer: c. Private members


Q8. If a base class is inherited in protected access mode then which among the following is true?
a. Public and Protected members of base class becomes protected members of derived class
b. Only protected members become protected members of derived class
c. Private, Protected and Public all members of base, become private of derived class
d. Only private members of base, become private of derived class

Answer: a. Public and Protected members of base class becomes protected members of derived class


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

These are Programming in Java Nptel Week 3 Assignment Answers


Q9. Which type of inheritance leads to diamond problem?
a. Single level
b. Multi-level
c. Multiple
d. Hierarchical

Answer: c. Multiple


Q10.What will be the output of the below program:

a. error: func() in subDemoClass cannot override func() in superDemoClass
b. value of b = 60
c. value of a = 20
d. None of the above

Answer: a. error: func() in subDemoClass cannot override func() in superDemoClass


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

These are Programming in Java Nptel Week 3 Assignment Answers

All Weeks of Programming In Java: Click Here

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

Programming in Java Nptel Week 3 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

These are Programming in Java Nptel Week 3 Assignment Answers


Q1. Which of the following statement is true regarding the order of execution of constructors in an inheritance hierarchy?
a. Base class constructor will be called followed by the derived class constructor.
b. Derived class constructor will be called followed by the base class constructor.
c. Only Base class constructor will be called.
d. Only derived class constructor will be called.

Answer: a. Base class constructor will be called followed by the derived class constructor.


Q2. The super() method is used to:
a. Call constructor of friend class
b. Is a declared method
c. Call constructor of the parent class
d. Call constructor

Answer: c. Call constructor of the parent class


Q3. What will be the output of the following Java program?
a. 0
b. 1
c. 2
d. Compilation Error

Answer: c. 2


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

These are Programming in Java Nptel Week 3 Assignment Answers


Q4. In Java, is it possible to override a static method?
a. Yes, we can override a static method just like we do with instance methods.
b. No, static methods cannot be overridden because they belong to the class, not the object.
c. It depends on whether the static method is declared as final or not.
d. It depends on the access modifier of the static method.

See also  Programming in Java | Week 1

Answer: b. No, static methods cannot be overridden because they belong to the class, not the object.


Q5. What is the output of the following Java program?
a. “The vehicle moves”
b. “The car moves”
c. The code does not compile
d. None of the above

Answer: b. “The car moves”


Q6. What is the output of the below Java program with inheritance?
a. Sweet=$10 Sugar=$20
b. Sweet=$10 Sugar=$10
c. Sweet=$20 Sugar=$20
d. Compiler error

Answer: a. Sweet=$10 Sugar=$20


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

These are Programming in Java Nptel Week 3 Assignment Answers


Q7. What is the purpose of method hiding in Java inheritance?
a. To prevent a subclass from inheriting methods
b. To override superclass methods with new implementations
c. To expose private methods of the superclass
d. To define methods with the same name in both the superclass and subclass

Answer: d. To define methods with the same name in both the superclass and subclass


Q8. What is the output of the following Java program?
a. “parent from parent”
b. “child from child”
c. “parent from child”
d. “child from parent”

Answer: c. “parent from child”


Q9. Can a class be marked as both “final” and “abstract” in Java?
a. Yes, but only if it has no methods.
b. Yes, a class can be marked as both “final” and “abstract.”
c. No, a class cannot be both “final” and “abstract.”
d. Yes, but only if it is marked as “protected.”

Answer: c. No, a class cannot be both “final” and “abstract.”


Q10. In Java, is it possible to override a static method?
a. Yes, we can override a static method just like we do with instance methods.
b. No, static methods cannot be overridden because they belong to the class, not the object.
c. It depends on whether the static method is declared as final or not.
d. It depends on the access modifier of the static method.

Answer: b. No, static methods cannot be overridden because they belong to the class, not the object.


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

These are Programming in Java Nptel Week 3 Assignment Answers

More Weeks of Programming In Java: Click here

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


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

Course Name: Programming In Java

Course Link: Click Here

These are Programming in Java Nptel Week 3 Assignment Answers


Programming Assignment

Question 1
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 .

Solution:

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

Question 2
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.

Solution:

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.print(d);
    }
}

These are Programming in Java Nptel Week 3 Assignment Answers


Question 3
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.

Solution:

double height;
	Test1(double length, double h) {
		super(length);
		height=h;
	}

	Test1(double length, double breadth, double h) {
		super(length,breadth);
		height=h;
	}

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

Question 4
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.

Solution:

QuestionScope st = new QuestionScope();
        int result1 = st.sum(n1,n2);
        int result2 = QuestionScope.multiply(n1,n2);

        System.out.println(result1);
        System.out.print(result2);

These are Programming in Java Nptel Week 3 Assignment Answers


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

Solution:

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

These are Programming in Java Nptel Week 3 Assignment Answers

More Weeks of Programming In Java: Click here

More Nptel Courses: Click here


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

Course Name: Programming in Java

Course Link: Click Here

These are Programming in Java Nptel Week 3 Assignment Answers


Q1. 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.

See also  Programming In Java | Week 2

Answer: a, b, d


Q2. 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


These are Programming in Java Nptel Week 3 Assignment Answers


Q3. Consider the following piece of code.
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, d


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

Answer: a. 0


These are Programming in Java Nptel Week 3 Assignment Answers


Q5. 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


Q6. 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


These are Programming in Java Nptel Week 3 Assignment Answers


Q7. Consider the following piece of code.
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.


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

Answer: b. ring


These are Programming in Java Nptel Week 3 Assignment Answers


Q9. 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.


Q10. 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


These are Programming in Java Nptel Week 3 Assignment Answers


Programming Assignment Solution

Question 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.

Solution:

//Code

These are Programming in Java Nptel Week 3 Assignment Answers


Question 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.

Solution:

//Code

These are Programming in Java Nptel Week 3 Assignment Answers


Question 3

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

Solution:

//Code

These are Programming in Java Nptel Week 3 Assignment Answers


Question 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 .

Solution:

//Code

These are Programming in Java Nptel Week 3 Assignment Answers


Question 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.

Solution:

//Code

These are Programming in Java Nptel Week 3 Assignment Answers

More Weeks of Programming In Java: Click Here

See also  Programming In Java | Week 3

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

Course Name: Programming in Java NPTEL

These are Programming in Java Nptel Week 3 Assignment Answers


Q1. Which of this keyword can be used in a sub class to call the constructor of super class?
a. super
b. this
c. extent
d. extends

Answer: a. super


Q2. What is the output of the above program?
a. i+jis 42 4
b. i+jis6 9 2
c. i+jis 42 9 2
d. i+jis 6 4

Answer: a. i+jis 42 4


Q3. What is the output of the above program?
a. 4
b. 10
c. 2
d. runtime error

Answer: c. 2


These are Programming in Java Nptel Week 3 Assignment Answers


Q4. For each description on the left, find the best matching modifier on the right. You may use a choice more than once or not at all.

a. 1-A, 2-A, 3-C, 4-D, 5-E
b. 1-A, 2-A, 3-A, 4-B, 5-C
c. 1-C, 2-B, 3-A, 4-A, 5-D
d. None of Above

Answer: b. 1-A, 2-A, 3-A, 4-B, 5-C


Q5. All the variables of interface should be?
a) default and final
b) default and static
c) public, static and final
d) protect, static and final

Answer: c) public, static and final


Q6. Which of the following statement(s) is/are NOT 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 overidden in a subclass.

Answer: d. A final method can be overidden in a subclass.


These are Programming in Java Nptel Week 3 Assignment Answers


Q7. Which of the following statements is/ are true?
a. Hello
b. There will be a compile-time error
c. HelloHello.
d. The program will give a runtime error.

Answer: d. The program will give a runtime error.


Q8. Which of the following option is true about the above program?
a. Eror: String cannot be a method return tpe like void, int, char, etc.; as it isa class.
b. Eror: Non-static variable ‘answer’ cannot be referenced from a static context.
c. Output: The answer to the question, Which course have you opted? is Programming with Java
d. Error: Compilation error as variable question’ is not static.

Answer: c. Output: The answer to the question, Which course have you opted? is Programming with Java


Q9. Disadvantage(s) of inheritance in Java programming is/are

a. Code readability
b. two classes (base and inherited class) get tightly coupled
c. Save development time and effort
d. Code reusability

Answer: b. two classes (base and inherited class) get tightly coupled


These are Programming in Java Nptel Week 3 Assignment Answers


Q10. Which inheritance in Java programming is not supported?
a. Multiple inheritance using classes.
b. Multiple inheritance using interfaces.
c. Multilevel inheritance.
d. Single inheritance.

Answer: a. Multiple inheritance using classes.


These are Programming in Java Nptel Week 3 Assignment Answers


Programming Assignment Solutions

Question 1

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 below.

Solution:

//Code

These are Programming in Java Nptel Week 3 Assignment Answers


Question 2

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.

Solution:

//Code

These are Programming in Java Nptel Week 3 Assignment Answers


Question 3

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.

Solution:

//Code

These are Programming in Java Nptel Week 3 Assignment Answers


Question 4

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.

Solution:

//Code

These are Programming in Java Nptel Week 3 Assignment Answers


Question 5

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

Solution:

//Code

These are Programming in Java Nptel Week 3 Assignment Answers