Week-02 Assignment PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

NPTEL Online Certification Courses

Indian Institute of Technology Kharagpur

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?

public class Question


{
public static void main(String[] args) {

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.

public class Main{


public static void main(String[] args) {
char[] copyFrom = { 'j', 'a', 'n', 'j', 'a', 'v', 'a',
'n', 'p', 't', 'e', 'l' };
char[] copyTo = new char[9];

System.arraycopy(copyFrom, 3, copyTo, 0, 9);


System.out.println(new String(copyTo));
}
}

What will be the output of the above program?


a. javanptel
b. npteljava
c. janjavanptel
d. janjavanp

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?

a. Using the string array provided as a parameter to the main method.


b. getText() method can be used to get user input from the command line.
c. Scanner class can be used by passing the predefined object System.in
d. Once the execution starts, there is no way to provide user input.

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?

a. Box obj = new Box();


b. Box obj = new Box;
c. obj = new Box();
d. new Box obj;

Correct Answer: a

Detailed Solution:
Others are invalid declarations.

______________________________________________________________________________

QUESTION 8:
Following is a program given for this question.

public class Question


{
public static void main(String[] args) {

boolean m=Integer.valueOf(1).equals(Long.valueOf(1));
System.out.println(m);
}
}

What will be the output of the above program?


a. false
b. true
c. 0
d. 1

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?

public class Question {


public static void main (String[] args) {
for (String s: args) {
System.out.println(s+args[0]);
}
}
}

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?

public class Main{


public static void main(String args[]){
char a = '3';
int b=011;
System.out.println(a+b);
}
}

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.

____________________________________________________________________________
******

You might also like