Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rank-math domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /opt/bitnami/wordpress/wp-includes/functions.php on line 6114
Programming In Java Nptel Week 5 Assignment Answers 2024

Programming in Java Nptel Week 5 Assignment Answers

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

Course Link: Click Here


Programming in Java Nptel Week 5 Assignment Answers
Programming in Java Nptel Week 5 Assignment Answers

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


1. Which exception will be thrown by parseInt() method in Java?
a. IntegerOutOfBoundException
b. IntegerFormatException
c. ArithmeticException
d. NumberFormatException

Answer: d. NumberFormatException


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

a) QQQQPPPP
PPPPQQQQ

b) PPPPQQQQ
QQQQPPPP

C) PPPPPPPP
QQQQQQQQ

D) Compilation error

Answer: a) QQQQPPPP
PPPPQQQQ


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


3. What will be the output of the following code?
a. 4
b. 6
c. 2
d. 8

Answer : a. 4


4. A method that potentially generates a checked exception must include this keyword in its method signature:
a. throw
b. extend
c. throws
d. extends

Answer: c. throws


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

These are Programming in Java Nptel Week 5 Assignment Answers


5.Which of the following statements is true about Java’s finally block?
a. The finally block is only executed if an exception is thrown in the try block
b. The finally block is only executed if an exception is thrown in the catch block
c. The finally block is only executed if an exception is not thrown in the try or catch block
d. The finally block is executed regardless of whether an exception is thrown in the try or catch block

Answer: d. The finally block is executed regardless of whether an exception is thrown in the try or catch block


6. Which of the following statements is true about exception handling in Java:
a. A try block can have many catch blocks but only one finally block
b. A try block can have many catch blocks and many finally blocks
c. A try block must have one finally block for each catch block
d. A try block must have at least one catch block to have a finally block

Answer: a. A try block can have many catch blocks but only one finally block



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


7. What will be the output of the following program?
a. Hello
b. World
c. HelloWOrld
d. WorldWorld

Answer: d. WorldWorld


8. What will be the output of the following Java code?
a. Hello
b. World
c. HelloWOrld
d. Compilation Error

Answer: c. HelloWOrld



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


9. What will be the output of the following Java program?
a. 0 0
b. 2 2
c. 4 1
d. 1 4

Answer: c. 4 1


10.Which of the following exceptions is not a subclass of the RuntimeException class?
a. NullPointerException
b. ArrayIndexOutOfBoundsException
c. IOException
d. ArithmeticException

Answer: c. IOException


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


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 5 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 of the following is an incorrect statement about interfaces?
a. Interfaces specifies what class must do but not how it does.
b. Interfaces are specified public if they are to be accessed by any code in the program.
c. All variables in interface are implicitly final and static.
d. All variables are static and methods are public if interface is defined public.

Answer: d. All variables are static and methods are public if interface is defined public.


Q2. How do you access a static method of an interface?
a. Using the interface name
b. Using the method name directly
c. Through an object of the interface
d. Through an implementation class

Answer: a. Using the interface name


Q3. What is the output of the below Java program with an Interface?
a. 1000
b. 2000
c. Compiler error
d. None of the above

Answer: c. Compiler error


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

These are Programming in Java Week 5 Solutions


Q4. What happens when we access the same variable defined in two interfaces implemented by the same class?
a. Compilation failure
b. Runtime Exception
c. The JVM is not able to identify the correct variable
d. The interfaceName.variableName needs to be defined

See also  Cyber Security and Privacy Week 5 Nptel Answers 2024

Answer: d. The interfaceName.variableName needs to be defined


Q5. Predict the output of following Java program
a. Got the Test Exception
Inside finally block
b. Got the Test Exception
c. Inside finally block
d. Compiler Error

Answer: a. Got the Test Exception
Inside finally block


Q6. What happens if an exception is not caught in the catch block?
a. The finally block handles it
b. The exception is ignored
c. The exception is thrown to the caller method
d. The program terminates immediately

Answer: c. The exception is thrown to the caller method


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

These are Programming in Java Week 5 Solutions


Q7. What will be the output of the following Java program?
a. Hello
b. World
c. HelloWorld
d. Hello World

Answer: b. World


Q8. What is the output of the below Java code with Exceptions?
a. Index 4 out of bounds for length 3
b. Index 4 out of bounds for length 3
Some exception
c. Some exception
d. No exception occurs

Answer: a. Index 4 out of bounds for length 3


Q9. What is the output of the Java code with FINALLY block and RETURN statement?
a. inside TRY
b. inside TRY
inside FINALLY
c. inside FINALLY
d. Compiler error

Answer: b. inside TRY
inside FINALLY


Q10. What is the purpose of the finally block in Java exception handling?
a. To handle an exception
b. To catch an exception
c. To clean up resources after a try block
d. None of the above

Answer: c. To clean up resources after a try block


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

These are Programming in Java Nptel Week 5 Answers

More Weeks of Programming In Java: Click here

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


Programming in Java Nptel Week 5 Assignment Answers (JULY-DEC 2023)

Course Name: Programming In Java

Course Link: Click Here

These are Programming in Java Nptel Week 5 Answers


Programming Assignment

Question 1
An interface Number is defined in the following program. You have to declare a class A, which will implement the interface Number. Note that the method findSqr(n) will return the square of the number n.

Solution:

class A implements Number {
	 int i, square;
	 public int findSqr(int i) {
		 square=i*i;
            return square;
      }
}

Question 2
This program is to find the GCD (greatest common divisor) of two integers writing a recursive function findGCD(n1,n2). Your function should return -1, if the argument(s) is(are) other than positive number(s).

Solution:

class B implements GCD {
    int n1,n2;

    public int findGCD(int n1, int n2){
		if(n1==0&& n2==0) {
			return -1;
		}
		else if(n2 == 0){
			return n1;
		}

		else {
			return findGCD(n2, n1%n2);
		}
      }
 }

Question 3
Complete the code segment to catch the ArithmeticException in the following, if any. On the occurrence of such an exception, your program should print “Exception caught: Division by zero.” If there is no such exception, it will print the result of division operation on two integer values.

Solution:

int result;
       a = input.nextInt();
       b = input.nextInt();

         try {
              result = a/b;
              System.out.print(result);
     	     }
          catch (ArithmeticException e) {
             System.out.print("Exception caught: Division by zero.");
          }

Question 4
In the following program, an array of integer data to be initialized. During the initialization, if a user enters a value other than integer value, then it will throw InputMismatchException exception. On the occurrence of such an exception, your program should print “You entered bad data.” If there is no such exception it will print the total sum of the array.

Solution:

try
      {
        for(int i=0;i<length;i++)
        {
          int userInput=sc.nextInt();
          name[i] = userInput;
          sum=sum+name[i];
        }
        System.out.print(sum);
       }
      catch(InputMismatchException e)
      {
        System.out.print("You entered bad data.");
      }

Question 5
In the following program, there may be multiple exceptions. You have to complete the code using only one try-catch block to handle all the possible exceptions.
For example, if user’s input is 1, then it will throw and catch “java.lang.NullPointerException“.

Solution:

try{
  switch (i)
  {
    case 0 :  int zero = 0;
    j = 92/ zero;
    break;
    case 1 :  int b[ ] = null;
    j = b[0] ;
    break;
    default:  System.out.print("No exception");
  }
}
catch (Exception e)
{
  System.out.print(e) ;
}

More Weeks of Programming In Java: Click here

More Nptel Courses: Click here


Programming in Java Nptel Week 5 Assignment Answers (JAN-APR 2023)

Course Name: Programming in Java

Course Link: Click Here

These are Programming in Java Week 5 Answers


Q1. Consider the following program.
If the program is executed, then what will be the output?

a. 22
b. 33
c. 23
d. 32

Answer: c. 23


Q2. Consider the following piece of code.
‘Which of the following statement(s) is/are true for the above code?

a. Itis an empty interface.
b. Itis a tag interface.
c. It is a marker interface.
d. Itis a nested interface.

Answer: a, b, c


Q3. What is the output of the following code?
a. java
10
b. java
20
c. 10
java
d. 20
java

See also  Programming In Java | Week 4

Answer: a. java
10


Q4. ‘Which of the following statement(s) is/are true?
a. All abstract methods defined in an interface must be implemented.
b. The variables defined inside an interface are static and final by default.
c. An interface is used to achieve full abstraction.
d. Inside an interface, a constructor can be called using the super keyword with hierarchy.

Answer: a, b, c


These are Programming in Java Week 5 Answers


Q5. Which of the following statement(s) is/are true?
1. A class can extend more than one class.
2. A class can extend only one class but many interfaces.
3. An interface can extend many interfaces.
4. An interface can implement many interfaces.
5. A class can extend one class and implement many interfaces.

a. 1 and 2
b. 2 and 4
c. 3 and 5
d. 3 and 4

Answer: c. 3 and 5


Q6. ‘Which of the following statement(s) is/are true?
a. Abstract class can have abstract and non-abstract methods.
b. Abstract class can have final, non-final, static and non-static vanables.
c. Interface has only static and final vaniables.
d. Interface can provide the implementation of an abstract class.

Answer: a, b, c


These are Programming in Java Week 5 Answers


Q7. Consider the following piece of code.
What is the output of the above code?

a. A
b. B
c. BC
d. AC

Answer: c. BC


Q8. The class at the top of exception class hierarchy is …………..
a. Object
b. Throwable
c. Exception
d. ArthmeticException

Answer: b. Throwable


These are Programming in Java Week 5 Answers


Q9. Which of these class is superclass of every class in Java?
a. Object class
b. Abstract class
c. String class
d AmayList class

Answer: a. Object class


Q10. If the program is executed, then what will be the output?
a. 012
b. 004
c. 024
d. 044

Answer: b. 004


Programming Assignment

Question 1

Complete the code segment to catch the ArithmeticException in the following, if any. On the occurrence of such an exception, your program should print “Exception caught: Division by zero.” If there is no such exception, it will print the result of division operation on two integer values.

Solution:

int result;
a = input.nextInt();
b = input.nextInt();
try{
    result = a/b;
    System.out.println(result);
}
catch (ArithmeticException e){
    System.out.println("Exception caught: Division by zero.");
}

Question 2

In the following program, an array of integer data to be initialized. During the initialization, if a user enters a value other than integer value, then it will throw InputMismatchException exception. On the occurrence of such an exception, your program should print “You entered bad data.” If there is no such exception it will print the total sum of the array.

Solution:

try{
    for(int i=0;i<length;i++){
        int userInput=sc.nextInt();
        name[i] = userInput;
        sum=sum+name[i];
        }
        System.out.println(sum);
}
catch(InputMismatchException e){
    System.out.println("You entered bad data.");
}

These are Programming in Java Week 5 Answers


Question 3

In the following program, there may be multiple exceptions. You have to complete the code using only one try-catch block to handle all the possible exceptions.
For example, if user’s input is 1, then it will throw and catch “java.lang.NullPointerException“.

Solution:

try{
    switch (i){
        case 0 :
            int zero = 0;
            j = 92/ zero;
            break;
            case 1 :
                int b[ ] = null;
                j = b[0] ;
                break;
                default:
                System.out.println("No exception");
            }
    }
    catch (Exception e){
        System.out.println(e);
    }

Question 4

An interface Number is defined in the following program. You have to declare a class A, which will implement the interface Number. Note that the method findSqr(n) will return the square of the number n.

Solution:

class A implements Number{
    int i, square;
    public int findSqr(int i){
        square=i*i;
        return square;
        }
}

These are Programming in Java Week 5 Answers


Question 5

This program is to find the GCD (greatest common divisor) of two integers writing a recursive function findGCD(n1,n2). Your function should return -1, if the argument(s) is(are) other than positive number(s).

Solution:

class B implements GCD{
    int n1,n2;
    public int findGCD(int n1, int n2){
        if(n1==0&& n2==0){
            return -1;
        }
        else if(n2 == 0){
            return n1;
        }
        else{
            return findGCD(n2, n1%n2);
        }
        }
}

These are Programming in Java Week 5 Answers

More Weeks of Programming In Java: Click Here

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


Programming in Java Nptel Week 5 Assignment Answers (JULY-DEC 2022)

Course Name: Programming in Java NPTEL

These are Nptel Programming in Java Week 5 Answers


Q1. Consider the following program.
If the program is executed, then what will be the output from the execution?

a. 100
102
b. 20
22
C. 102
100
d. 22
20

Answer: a. 100 102


These are Nptel Programming in Java Week 5 Answers


Q2. Consider the following program.
If the program is executed, then what will be the output from the execution?
a. 170
b. 130
c. 0
d. 260

Answer: a. 170


These are Nptel Programming in Java Week 5 Answers


Q3. What is the output of the following code?
a. Output: This is Explanation’s Print method
This is Answer’s Print method
b. Error: super.super’ is not allowed.
c. Error: Compilation unsuccessful, as there is only one super class of Answer
d. Output: This is Answer’s Print method
This is Explanation’s Print method

See also  Ethical Hacking Nptel Week 5 Assignment Answers

Answer: b. Error: super.super’ is not allowed.


Q4. Which of the following is/are interface(s)?
a. DriverManager
b. Connection
c. Statement
d. ResultSet

Answer: b, c, d


These are Nptel Programming in Java Week 5 Answers


Q5. Which of the following statement(s) is/are true?
a. You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
b. You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
c. A subclass inherits all of the public and protected members of its parent, 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


Q6. 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 subelass. /
c. You can prevent a class from being sub classed 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 Nptel Programming in Java Week 5 Answers


Q7. Consider the following piece of code:
Which of the following statement(s) is/are correct?
a. There is no main () method so the program is not compile successfully.
b. The value of i will be printed as 22, as it is static and final by default.
c. The value of I will be printed as 2. as it is initialized in class B.
d. Compile time error

Answer: a. There is no main () method so the program is not compile successfully.


Q8. Which of the following statement(s) is/are true?
a. A class can extend more than one class.
b. An interface can extend many interfaces.
c. An interface can implement many interfaces.
d. A class can extend one class and implement many interfaces.

Answer: b, d


These are Nptel Programming in Java Week 5 Answers


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


Q10. If the program is executed, then what will be the output from the execution?
a. Output: 1020
b. Output : 30
c. Output: 2010
d. Error: Cl is abstract; cannot be instantiated.

Answer: d


These are Nptel Programming in Java Week 5 Answers


Programming Assignment

Question 1
An interface Number is defined in the following program. You have to declare a class A, which will implement the interface Number. Note that the method findSqr(n) will return the square of the number n.

Solution:

//Code

These are Programming in Java Week 5 Solutions


Question 2
This program is to find the GCD (greatest common divisor) of two integers writing a recursive function findGCD(n1,n2). Your function should return -1, if the argument(s) is(are) other than positive number(s).

Solution:

//Code

These are Programming in Java Week 5 Solutions


Question 3
Complete the code segment to catch the ArithmeticException in the following, if any. On the occurrence of such an exception, your program should print “Exception caught: Division by zero.” If there is no such exception, it will print the result of division operation on two integer values.

Solution:

//Code

These are Programming in Java Week 5 Solutions


Question 4
In the following program, an array of integer data to be initialized. During the initialization, if a user enters a value other than integer value, then it will throw InputMismatchException exception. On the occurrence of such an exception, your program should print “You entered bad data.” If there is no such exception it will print the total sum of the array.

Solution:

//Code

These are Programming in Java Week 5 Solutions


Question 5
In the following program, there may be multiple exceptions. You have to complete the code using only one try-catch block to handle all the possible exceptions.
For example, if user’s input is 1, then it will throw and catch “java.lang.NullPointerException“.

Solution:

//Code

These are Programming in Java Week 5 Solutions