Programming In Java | Week 5

Programming In Java Week 5 Answers NPTEL


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

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

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.

Q3. What is the output of the following code?
a. java

b. java
c. 10
d. 20

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.

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

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.

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

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

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

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

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


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.


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

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.


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

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


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

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.


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

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


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;
            return findGCD(n2, n1%n2);

* The material and content uploaded on this website are for general information and reference purposes only and don’t copy the answers of this website to any other domain without any permission or else copyright abuse will be in action.

Please do it by your own first! Protection Status

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
close button
Would love your thoughts, please comment.x