NPTEL Programming In Java Assignment 3 Answers July 2023

NPTEL Programming In Java Assignment 3 Answers July 2023:- All the Answers are provided here to help the students as a reference, You must submit your assignment at your own knowledge

ALSO READ :-
NPTEL Registration Steps [July – Dec 2022]
NPTEL Exam Pattern Tips & Top Tricks [2022]
NPTEL Exam Result 2022 | NPTEL Swayam Result Download

NPTEL Programming In Java Week 3 Quiz Assignment Answers 2023

1. In which of the following scenario(s), the static block is used in Java?

a. To create static variables.
b. To initialize instance variables.
c. To initialize static variables.
d. To create new objects.

Answer :- c

2. Consider the following piece of code.

public static void main(String[] args) {
    incrementCount();
    System.out.println("Count: " + count);
}

___________ incrementCount() {
    count++;
}
}

Fill in the blank with the appropriate keyword(s) from the list given below so that the program compiles successfully.

a. public void
b. private void
c. public static void
d. private static void

Answer :- c, d

3. Consider the following piece of code.

class A {
    public void display() {
        System.out.println("A's display method");
    }
}

class B extends A {
    public void display() {
        System.out.println("B's display method");
    }
}

public class Main {
    public static void main(String[] args) {
        A a = new B();
        a.display();
        ((B) a).display();
    }
}

What is the output of the above code?

a. A’s display method
B’s display method
b. A’s display method
A’s display method
c. B’s display method
B’s display method
d. B’s display method
A’s display method

Answer :- c

4. Which of the following statements) is/are false?

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 :- d

5. Which of the following statement(s) is/are true?

a. 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.
b. You can prevent a class from being subclassed by using the final keyword in the class’s declaration.
c. An abstract class can be instantiated.
d. Common behaviour can be defined in a superclass and inherited into a subclass using the extends keyword.

Answer :- a, b, d

6. Consider the following program.

public class Question {
    public static void main(String[] args) {
        String str = " programming in java ";
        System.out.println(str.substring(1, 3)+str.substring(4, 5) + 
         str.substring(6, 8));
    }
}

What is the output of the above program?

a. prgam
b. program
c. gramm
d. ing in

Answer :- a

7. Consider the following piece of code.

NPTEL Programming In Java Assignment 3 Answers July 2023

Which of the following is the output of the above program?

a. 10
100
b. 10
20
c. 100
10
d. 10
10

Answer :- a

8. Consider the following program.

class Question {
    int a = 400;
    int b = 200;
}

class Child extends Question {
    int a = 1000;
    int b = 2000;

    void add(int a, int b) {
        System.out.println(a + this.b - super.a);
    }
}

public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.add(100, 300);
    }
}

If the program is executed, then what will be the output from the execution?

a. 1700
b. 1300
c. 0
d. 2600

Answer :- a

9. Which of the following statement(s) is/are true?

a. Hiding internal data from the outside world and accessing it only through publicly exposed methods is known as data encapsulation.
b. Static methods in interfaces are never inherited.
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 :- a, b, d

10. All classes in java are inherited from which class?

a. java.lang.class
b. java.class.inherited
c. java.class.object
d. java.lang.Object

Answer :- d

NPTEL Programming In Java Week 3 Programming Assignment Solutions 2023

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

import java.util.Scanner; //This package for reading input
public class Fibonacci { 

public static void main(String args[]) { 
	 Scanner sc = new Scanner(System.in);
	int n=sc.nextInt(); //Read an integer
System.out.println(fib(n)); //Generate and print the n-th Fibonacci                
                                     //number
    } 
static int fib(int n) {
 if (n==1)      //Terminal condition
    	return 0;
    else if(n==2)
    	return 1;
    return fib(n - 1) + fib(n - 2); //Recursive call of function
   }
}

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

import java.util.Scanner;

public class Circle extends Point{

public static void main(String[] args) {

	 Scanner sc = new Scanner(System.in);
	 Point p1=new Point();
	 p1.x=sc.nextDouble();
	 p1.y=sc.nextDouble();
	 Point p2=new Point();
	 p2.x=sc.nextDouble();
	 p2.y=sc.nextDouble();
	 Circle c1=new Circle();
	c1.distance(p1,p2);

  }

}

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

Q3. 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:-

import java.util.Scanner;
class Shape{
    double length, breadth;
    Shape(double l, double b){ //Constructor to initialize a Shape object
        length = l;
        breadth= b;
    }
    Shape(double len){    //Constructor to initialize another Shape object
        length = breadth = len;
    }
    double calculate(){// To calculate the area of a shape object
        return length * breadth ;
    }
}
public class Test1 extends Shape{

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

	public static void main(String args[]){
    	Scanner sc = new Scanner(System.in);//Create an object to read
                                              //input
    	double l=sc.nextDouble(); //Read length
    	double b=sc.nextDouble(); //Read breadth
    	double h=sc.nextDouble(); //Read height
    	Test1 myshape1 = new Test1(l,h);
    	Test1 myshape2 = new Test1(l,b,h);
    	double volume1;
    	double volume2;
    	volume1 = myshape1.calculate();
    	volume2=myshape2.calculate();
    	System.out.println(volume1);
    	System.out.println(volume2);
	}
}

Q4. 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:-

import java.util.Scanner;
class QuestionScope {
    int sum(int a, int b){ //non-static method
            return a + b;
        }
    static int multiply(int a, int b){ //static method
            return a * b;
        }
}
public class Exercise3_4{
    public static void main( String[] args ) {
        Scanner sc = new Scanner(System.in);
		int n1=sc.nextInt();
		int n2=sc.nextInt();

        //Called the method sum() to find the sum of two numbers.
        //Called the method multiply() to find the product of two numbers.

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

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

Solution:-

import java.util.Scanner;
class Question {  //Define a class Question with two elements e1 and e2.
    Scanner sc = new Scanner(System.in);
    int e1 = sc.nextInt();  //Read e1
    int e2 = sc.nextInt();  //Read e2
}
public class Question3 {
// Define static method swap()to swap the values of e1 and e2 of class Question.

    public static void swap(Question t)
    {
    	int temp = t.e1;
        t.e1 = t.e2;
        t.e2 = temp;
    }
    public static void main(String[] args) {
        //Create an object of class Question
    	Question t = new Question ();
        //Call the method swap()
        swap(t);
        System.out.println(t.e1+" "+t.e2);
    }
}

NPTEL Programming In Java Assignment 3 Answers [July 2022]

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

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

Answers will be Uploaded Shortly and it will be Notified on Telegram, So JOIN NOW

NPTEL Programming In Java Assignment 3 Answers July 2023

3. What is the output of the above program?

a. 4
b. 10
c. 2
d. runtime error

Answer:- c

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

1. Hides the instance variable from code in other files. A. private
2. Hides the method from code in other files B. public
3. Hides the subclass from code in other files. C. final
4. Exposes the API method to code in other files. D. static
5. Prevents the value of the instance variable from being
Changed once initialized. E. none of the above

a. 1-A.2-A.3-C.4-D5-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

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

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

👇For Week 04 Assignment Answers👇

NPTEL Programming In Java Assignment 3 Answers July 2023

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

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

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

10. 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:- For Answer Click Here

NPTEL Programming In Java Assignment 3 Programming Solutions

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

Code:-

//complete the code segment to find the nth Fibonacci number in the Fibonacci sequence and return the value. Write the function recursively.

   if (n==1)    //terminal condition
     return 0;
     else if(n==2)
     return 1;
return fib(n - 1) + fib(n - 2); //Recursive call of function

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

Code:-

//Complete the code segment to define a class Point with parameter x,y and method distance()for calculating distance between two points.
//Note: Pass objectsof type class Point as argument in distance() method. 

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

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

Code:-

//Create a derived class constructor which can call the one parametrized constructor of the base class
//Create a derived class constructor which can call the two parametrized constructor of the base class
//Override the method calculate() in the derived class to find the volume of a shape instead of finding the area of a shape

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

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

Code:-

//Called the method sum() to find the sum of two numbers.
//Called the method multiply() to find the product of two numbers.

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

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

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

Code:-

// Define static method swap()to swap the values of e1 and e2 of class Question.

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

What is Programming In Java?

With the growth of Information and Communication Technology, there is a need to develop large and complex software. Further, those software should be platform independent, Internet enabled, easy to modify, secure, and robust. To meet this requirement object-oriented paradigm has been developed and based on this paradigm the Java programming language emerges as the best programming environment. Now, Java programming language is being used for mobile programming, Internet programming, and many other applications compatible to distributed systems. This course aims to cover the essential topics of Java programming so that the participants can improve their skills to cope with the current demand of IT industries and solve many problems in their own filed of studies.

CRITERIA TO GET A CERTIFICATE

Average assignment score = 25% of the average of best 8 assignments out of the total 12 assignments given in the course.
Exam score = 75% of the proctored certification exam score out of 100

Final score = Average assignment score + Exam score

YOU WILL BE ELIGIBLE FOR A CERTIFICATE ONLY IF THE AVERAGE ASSIGNMENT SCORE >=10/25 AND EXAM SCORE >= 30/75. If one of the 2 criteria is not met, you will not get the certificate even if the Final score >= 40/100.

NPTEL Programming In Java Assignment 2 Answers Jan 2022

Q1. Consider the following piece of code in Java.

class A {publicint i;protectedint j;}class B extends A
        {void display(){super.j =super.j-super.i;System.out.println(super.i +" "+super.j);}}publicclass inheritance
        {publicstaticvoid main(String args[]){
            B obj =new B();
            obj.i=3;
            obj.j=6;
            obj.display();}}

What is the output of the above program?

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

Answer:- b) 3 3

👇FOR NEXT WEEK ASSIGNMENT ANSWERS👇

NPTEL Programming In Java Assignment 3 Answers July 2023

Q2. Consider the following piece of code in Java.

publicclassQuestion{publicstaticint x =7;publicstaticvoid main(String[] args){Question a =newQuestion();Question b =newQuestion();
       a.x =2;
       b.x =2;System.out.println(a.x+b.x+Question.x);}}

What is the output of the above program?

a) 6
b) 10
c) 21
d) error

Answer:- a) 6

Q3. If a class inheriting an abstract class does not define all of its functions then it will be known as?

a) Default
b) Abstract
c) A simple class
d) Static class

Answer:- b) Abstract

Q4. Which among the following best describes polymorphism?

a) It is the ability for many messages/data to be processed in one way
b) It is the ability for a message/data to be processed in only 1 form
c) It is the ability for a message/data to be processed in more than one form
d) It is the ability for undefined message/data to be processed in at least one way

Answer:- c) It is the ability for a message/data to be processed in more than one form

Q5. Consider the following piece of code in Java

classMen{int walkingDistance(int weight){System.out.println(10);return10;}}classWildMenextendsMen{void walkingDistance(int weight){System.out.println("20");}}publicclassMethodOverriding3{publicstaticvoid main(String[] args){WildMen wc =newWildMen();
    wc.walkingDistance(30);}}

What is the output of the above program?

a) 30
b) 20
c) Compile error
d) Runtime error

Answer:- c) Compile error

Q6. All the variables of the 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

👇FOR NEXT WEEK ASSIGNMENT ANSWERS👇

NPTEL Programming In Java Assignment 3 Answers July 2023

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

a) Code readability
b) two classes (base and inherited class) get tightly coupled
c) Code maintainability
d) Code reusability

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

Q8. When does method overloading is determined?

a) At run time
b) At coding time
c) At compile time
d) At execution time

Answer:- c) At compile time

Q9.

publicclassTest1{Test1(){Test1 obj1 =newTest1();}publicstaticvoid main(String[]args){Test1 obj =newTest1();System.out.println("Hello");}}

Which of the following statements is/are true?

a) Output: Hello
b) Program will compile successfully
c) There will be a compile-time error
d) The program will give a runtime error

Answer:- (B) & (D)

Q10. Which of the following statement(s) is/are true?

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 static field
d) A local variable stores temporary state; it is declared inside a method

Answer:- (C) & (D)

👇FOR NEXT WEEK ASSIGNMENT ANSWERS👇

NPTEL Programming In Java Assignment 3 Answers July 2023

Disclaimer:- We do not claim 100% surety of solutions, these solutions are based on our sole expertise, and by using posting these answers we are simply looking to help students as a reference, so we urge do your assignment on your own.

For More NPTEL Answers:- CLICK HERE

Join Our Telegram:- CLICK HERE

Programming In Java Assignment 2 Answers 2022:- All the Answers provided here to help the students as a reference, You must submit your assignment at your own knowledge

If you found this article Interesting and helpful, don’t forget to share it with your friends to get this information.

Leave a Comment