# NPTEL Programming In Java Assignment 5 Answers 2022

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

## What is Programming In Java?

With the growth of Information and Communication Technology, there is a need to develop large and complex software. Further, that 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, the 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.

Below you can find the answers for NPTEL Programming In Java Assignment 5

## NPTEL Programming In Java Assignment 5 Answers July 2022

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

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

3. What is the output of the following code?

a. Output: This is Explanation’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

`Answer:- b`

4. Which of the following is/are interface(s)?

a. DriverManager
b. Connection
c. Statement
d. ResultSet

`Answer:- b, c, d`

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

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

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

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

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

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

### Programming Assignment

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

Code:-

``````class A implements Number
{

int sq_side;
int square;
public int findSqr(int sq_side)
{
square=sq_side*sq_side;
return square;
}
}
``````

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

Code:-

``````class B implements GCD {
int number_1,number_2;

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

else {
return findGCD(number_2, number_1%number_2);
}
}
}``````

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

Code:-

``````       int Result_of_divide ;
a = input.nextInt();
b = input.nextInt();

try
{
Result_of_divide= a/b;

System.out.print(Result_of_divide);
}

catch (ArithmeticException e)
{
System.out.print("Exception caught: Division by zero.");
}``````

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

Code:-

``````try{
for(int z1=0;z1<length;z1++)
{
int Input_given_user=sc.nextInt();
name[z1] = Input_given_user;
sum=sum+name[z1];
}
System.out.print(sum);
}
catch(InputMismatchException e)
{
}``````

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

Code:-

``````try
{
switch (i)
{
case 0 :
int zero_value = 0;
j = 9685/ zero_value;
break;
case 1 :
int b[ ] = null;
j = b ;
break;
default:
System.out.print("No exception");
}
}

catch (Exception f)
{
System.out.print(f) ;
}
``````

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

Code:-

``````//Create a class A which implements the interface Number.
class A implements Number {
//Define a method to find the square of a number
int i, square;
public int findSqr(int i) {
square=i*i;
return square;
}
}``````

Q2. This program isto 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).

Code:-

``````//Create a class B, which implements the interface GCD.
class B implements GCD {
int n1,n2;

//Create a method to calculate GCD
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);
}
}
}``````

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

Code:-

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

// try block to divide two numbers and display the result
try {
result = a/b;
System.out.println(result);
}
// catch block to catch the ArithmeticException
catch (ArithmeticException e) {
System.out.println("Exception caught: Division by zero.");
}

``````

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

Code:-

``````//Define try-catch block to save user input in the array "name",if there is an exception then catch the exception otherwise print the total sum of the array.
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) {
}
``````

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

Code:-

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