
☞ Programming in Java
NPTEL Week 05 : Programming Assignments Answers- 2025
import java.util.Scanner;
interface Number {
int findSqr(int i); // Returns the square of n
}
// Answer
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;
}
}
//End
public class W05_P1{
public static void main (String[] args){
A a = new A(); //Create an object of class A
// Read a number from the keyboard
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
System.out.print(a.findSqr(i));
}
}
import java.util.Scanner;
interface GCD {
public int findGCD(int n1,int n2);
}
// Answer
class B implements GCD {
// Create a method to calculate GCD
public int findGCD(int n1, int n2) {
if (n1 == 0 && n2 == 0) {
return 0;
} else if (n2 == 0) {
return n1;
} else {
return findGCD(n2, n1 % n2);
}
}
}
//End
public class W05_P2{
public static void main (String[] args){
B a = new B(); //Create an object of class B
// Read two numbers from the keyboard
Scanner sc = new Scanner(System.in);
int p1 = sc.nextInt();
int p2 = sc.nextInt();
System.out.print(a.findGCD(p1,p2));
}
}
import java.util.Scanner;
public class W05_P3 {
public static void main(String[] args) {
int a, b;
Scanner input = new Scanner(System.in);
// Read any two values for a and b
int result;
a = input.nextInt();
b = input.nextInt();
// Answer
//Get the result of a/b;
//# try block to divide two numbers and display the result
try {
result = a / b;
System.out.print(result);
}
//# catch block to catch the ArithmeticException
catch (ArithmeticException e) {
System.out.print("Exception caught: Division by zero.");
}
// End
}
}
//Prefixed Fixed Code:
import java.util.Scanner;
import java.util.InputMismatchException;
public class W05_P4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int length = sc.nextInt();
// create an array to save user input
int[] name = new int[length];
int sum=0;//save the total sum of the array.
// ANSWER
/*
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++)
{
name[i] = sc.nextInt();
sum = sum + name[i];
}
System.out.print(sum);
}
catch(InputMismatchException e) {
System.out.print("You entered bad data.");
}
// END
}
}
import java.util.Scanner;
public class W05_P5{
public static void main (String args[ ] ) {
Scanner scan = new Scanner(System.in);
int i=scan.nextInt();
int j;
//ANSWER
// Put the following code under try-catch block to handle exceptions
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 (NullPointerException ne)
{
System.out.print("java.lang.NullPointerException");
}
catch (ArithmeticException ae)
{
System.out.print("java.lang.ArithmeticException: / by zero");
}
//END
}
}
☞ NPTEL - Programming in Java - QUIZ : Week 12:Assignment 12 Answers- 2025
☞ NPTEL - Programming in Java - Week 12 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 11:Assignment 11 Answers- 2025
☞ NPTEL - Programming in Java - Week 11 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 10:Assignment 10 Answers- 2025
☞ NPTEL - Programming in Java - Week 10 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 9:Assignment 9 Answers- 2025
☞ NPTEL - Programming in Java - Week 09 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 8:Assignment 8 Answers- 2025
☞ NPTEL - Programming in Java - Week 08 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 7:Assignment 7 Answers- 2025
☞ NPTEL - Programming in Java - Week 07 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 6:Assignment 6 Answers- 2025
☞ NPTEL - Programming in Java - Week 06 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 5:Assignment 5 Answers- 2025
☞ NPTEL - Programming in Java - Week 05 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 4:Assignment 4 Answers- 2025
☞ NPTEL - Programming in Java - Week 04 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 3:Assignment 3 Answers- 2025
☞ NPTEL - Programming in Java - Week 03 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 2:Assignment 2 Answers- 2025
☞ NPTEL - Programming in Java - Week 02 : Programming Assignments Answers- 2025
☞ NPTEL - Programming in Java - QUIZ : Week 1:Assignment 1 Answers- 2025
☞ NPTEL - Programming in Java - Week 01 : Programming Assignments Answers- 2025
☞ NPTEL - Introduction to Programming in C - Week 7:Assignment 7 Answers- 2025
☞ NPTEL - Introduction to Programming in C - Week 6:Assignment 6 Answers- 2025
☞ NPTEL - Introduction to Programming in C - Week 5:Assignment 5 Answers- 2025
☞ NPTEL - Introduction to Programming in C - Week 4:Assignment 4 Answers- 2025
☞ NPTEL - Introduction to Programming in C - Week 3:Assignment 3 Answers- 2025
☞ NPTEL - Introduction to Programming in C - Week 2:Assignment 2 Answers- 2025
☞ NPTEL - Introduction to Programming in C - Week 1:Assignment 1 Answers- 2025