Week-02 Assignment PDF
Week-02 Assignment PDF
Week-02 Assignment PDF
PROGRAMMING IN JAVA
Assignment2
TYPE OF QUESTION: MCQ
Number of questions: 10 Total mark: 10 × 1 = 10
______________________________________________________________________________
QUESTION 1:
What is the output of the following program?
int i = 10;
int n = i++%5;
int m = ++i%5;
System.out.println(i+n+m);
}
}
a. 14
b. 13
c. 15
d. 11
Correct Answer: a
____________________________________________________________________________
QUESTION 2:
During constructor overloading, which of the following should be used in a parameterized
constructor to call the default constructor?
a. The parameterized constructor should be declared final in order for it to call the default
constructor.
b. The this() reference should be used as the first statement inside the parameterized
constructor.
c. The this() reference should be used anywhere inside the parameterized constructor.
d. It is not possible to implicitly call the default constructor from parameterized constructor.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Correct Answer: b
Detailed Solution:
The this() reference should be used as the first statement inside the parameterized constructor
in order to redirect
____________________________________________________________________________
QUESTION 3:
Following is a program given for this question.
Correct Answer: a
Detailed Solution:
______________________________________________________________________
QUESTION 4:
Which of the following can be used to take input from user during the execution of a
program?
Correct Answer: c
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Detailed Solution:
The easiest way to read input in a Java program during execution is by using the Scanner class in
java.util package. It can be used for obtaining the input of the primitive types like int, double, etc.
and strings. The argument values provided in the main method is only applicable when the
execution starts but during execution no value can be passed in that argument.
____________________________________________________________________________
QUESTION 5:
Which of the following is/are TRUE about print() and println() methods?
a. print() prints in a single line only and multiple lines cannot be printed in any way.
b. println()prints and then appends a line break.
c. println() prints in a single line only and multiple lines cannot be printed.
d. print() prints and then appends a line break.
Correct Answer: b
Detailed Solution:
Method print() can be used to print in a single line only but multiple lines can be printed using
escape sequence ‘\n’. Similarly, println() prints in a single line only and multiple lines can be
printed using escape sequence ‘\n’. Method print() prints but does not appends a line break. So,
option (b) println() prints and then appends a line break is the only correct option.
____________________________________________________________________________
QUESTION 6:
Which of the following is called when a method having the same name as that the name of
the class where it is defined?
a. abstract
b. this
c. constructor
d. final
Correct Answer: c
Detailed Solution:
In a class, if more than one method having the same name but with different signature is used,
then it is called a constructor.
______________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 7:
Which of the following is a valid declaration of an object of class, say Box?
Correct Answer: a
Detailed Solution:
Others are invalid declarations.
______________________________________________________________________________
QUESTION 8:
Following is a program given for this question.
boolean m=Integer.valueOf(1).equals(Long.valueOf(1));
System.out.println(m);
}
}
Correct Answer: a
Detailed Solution:
The two objects (the Integer and the Long) have different types.
______________________________________________________________________________
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 9:
What will happen during the execution of the following code for the command line input?
Consider the following input on command line and select the options with the correct output(s).
Input:
A: “jan java nptel”
B: 1 2 3
a. A : jannptel
javanptel
nptelnptel
b. A : jan java nptel jan java nptel
c. B : 11
21
31
d. B : 1 2 3 1
Correct Answer: b, c
Detailed Solution:
Java interpreted as a single argument, if the command line input is enclosing within quotation
marks.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 10:
What is the output of the following program?
a. 60
b. 3011
c. 33
d. 311
Correct Answer: a
Detailed Solution:
The argument will take the + operator as the arithmetic addition on the ASCII values instead of
characters.
____________________________________________________________________________
******