Table of Contents
NPTEL Programming in Java Assignment 5 Answers (Week 5)
Q1. Consider the following program.
class Question{
int i;
}
class Test extends Question{
int j;
void display () {
super.i = j + 1;
System.out.println(j + " " + i);
}}
public class Query{
public static void main(String args[]) {
Test obj = new Test();
obj.i=1;
obj.j=2;
obj.display();
}}
If the program is executed, then what will be the output?
Answer: c. 2 3
Follow us for daily updates!
Q2. Consider the following piece of code.
package java.util;
public interface EventListener
{
}
Which of the following statement(s) is/are true for the above code?
Answer: a. It is an empty interface.
b. It is a tag interface.
c. It is a marker interface.
Q3. What is the output of the following code?
interface A {
int x=10;
void m1();
}
class B implements A {
int x = 20;
public void m1() {
System.out.println("java");
}
}
public class Test {
public static void main(String[] args) {
A a = new B();
a.m1();
System.out.println(a.x);
}
}
Answer: a. java
10
Q4. Which of the following statement(s) is/are true?
Answer: 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.
Q5. Which of the following statement(s) is/are true?
- A class can extend more than one class.
- A class can extend only one class but many interfaces.
- An interface can extend many interfaces.
- An interface can implement many interfaces.
- A class can extend one class and implement many interfaces.
Answer: c. 3 and 5
Q6. Which of the following statement(s) is/are true?
Answer: a. Abstract class can have abstract and non-abstract methods.
c. Interface has only static and final variables.
Q7. Consider the following piece of code.
public class Question {
public static void main(String args[]) {
try {
int a, b;
b = 0;
a = 25/b;
System.out.print ("A"); }
catch (ArithmeticException e) {
System.out.print ("B");
}
finally
{
System.out.print ("C");
}}}
What is the output of the above code?
Answer: c. BC
Q8. The class at the top of exception class hierarchy is
Answer: b. Throwable
Q9. Which of these class is superclass of every class in Java?
Answer: a. Object class
Q10.
interface calculate(
int VAR= 0;
void cal (int item);
}
class display implements calculate{
int x;
public void cal (int item) {
if (item<2)
x = VAR;
else
x = item * item;
}
}
public class Question {
public static void main(String args[]) {
display[] arr=new display [3];
for (int i=0;i<3;i++)
arr[i]=new display();
arr[0].cal(0);
arr[1].cal (1);
arr [2].cal (2);
System.out.print (arr[0].x+" " + arr[1].x + " " + arr [2].x);
}
}
If the program is executed, then what will be the output?
Answer: b. 0 0 4
NPTEL Programming in Java Assignment 5 Programming Answers (Week 5)
Q1.
int ans;
a = input.nextInt();
b = input.nextInt();
try {
ans= a/b;
System.out.print(ans);
}
catch (ArithmeticException e)
{
System.out.print("Exception caught: Division by zero.");
}
Q2.
try{
for(int y=0;y<length;y++)
{
int userInput=sc.nextInt();
name[y] = userInput;
sum+=name[y];
}
System.out.print(sum);
}
catch(InputMismatchException e)
{
System.out.print("You entered bad data.");
}
Q3.
try
{
switch (i)
{
case 0 :
int jio = 0;
j = 92/ jio;
break;
case 1 :
int b[ ] = null;
j = b[0] ;
break;
default:
System.out.print("No exception");
}
}
catch (Exception e)
{
System.out.print(e) ;
}
Q4.
class A implements Number
{
int k;
int square;
public int findSqr(int k)
{
square=k*k;
return square;
}
}
Q5.
class B implements GCD {
int no1,no2;
public int findGCD(int no1, int no2){
if(no1==0&& no2==0)
{
return(-1);
}
else if(no2 == 0)
{
return(no1);
}
else {
return findGCD(no2, no1%no2);
}
}
}
Disclaimer: These answers are provided only for the purpose to help students to take references. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself.
Also Available: