NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
PROGRAMMING IN JAVA
Assignment 2
TYPE OF QUESTION: MCQ
Number of questions: 10 Total mark: 10 × 1 = 10
______________________________________________________________________________
QUESTION 1:
Following is a program given for this question.
public class Question{
public static void main(String[] args){
int[] x = {222, 210, 012};
for(int i = 0; i < x.length; i++){
System.out.print(x[i] + "");
}
}
}
What will be the output of the above program?
a. 22221010
b. 12222101
c. 22101010
d. 22221012
Correct Answer: a
Detailed Solution:
Since, int[2] numeral value have 0 in leading, therefore, it's taken as octal base (8). So, it's
corresponding decimal value is (2 × 80) + (1 × 81) = 10.
QUESTION 2:
When an array is passed to a method, what value does the method receive?
a. Reference of the array.
b. Copy of the array.
c. First element in the array.
d. Length of the array.
Correct Answer: a
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
Detailed Solution:
Reference of the array is passed to a method in Java.
____________________________________________________________________________
QUESTION 3:
Following is a program given for this question.
public class Main{
public static void main(String args[]){
byte x = 28;
x++;
x++;
System.out.print(x);
}
}
What will be the output of the above program?
a. 28
b. -29
c. 30
d. -31
Correct Answer: c
Detailed Solution:
The increment operator ++ works here in the normal scenario as there is nor assignment
operator. So, it will not give any difference between ++x and x++.
QUESTION 4:
How many bits are needed for float and double in Java, respectively?
a. 32 and 64
b. 32 and 32
c. 64 and 64
d. 64 and 32
Correct Answer: a
Detailed Solution:
Float needs 4 bytes or 32 bits and Double needs 8 bytes or 64 bits.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
QUESTION 5:
Which of the following is a valid automatic type conversion in Java?
a. short to byte
b. float to long
c. int to short
d. int to long
Correct Answer: d
Detailed Solution:
Automatic type conversation is possible in the following sequence (reverse is not possible):
byte → short → int → long → float → double
QUESTION 6:
Consider the following program and identify the output.
public class Question{
public static void main(String[] args){
short x = 10;
x = x * 5;
System.out.print(x);
}
}
a. 5
b. 10
c. 50
d. Compilation error
Correct Answer: d
Detailed Solution:
Compilation Error in “x = x * 5;”. This is due to lossy conversion from int to short. It works
if the type casting is followed, for example, x = (short) ( x * 5);
QUESTION 7:
Which of the following is a valid declaration of an object of class say, Student?
a. Student obj = new Student;
b. Student obj = new Student();
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
c. obj = new Student();
d. new Student obj;
Correct Answer: b
Detailed Solution:
When an object is to be declared it needs to be instantiated, hence the correct syntax of declaring
an object is (b).
______________________________________________________________________________
QUESTION 8:
What is the output of the following program?
public class Question{
public static void main(String[] args){
int[] A = {0,1,2};
for(int i = 0; i < A.length; i++){
A[i] = A[(A[i] + 3) % A.length];
}
for(int i = 0; i < A.length; i++){
System.out.print(A[i]);
}
}
}
a. 210
b. 120
c. 012
d. 201
Correct Answer: c
Detailed Solution:
The output after execution is 012.
QUESTION 9:
Consider the following piece of code.
NPTEL Online Certification Courses
Indian Institute of Technology Kharagpur
public class Question{
public static void main(String[] args){
String str = “anpctdelqjpava”;
System.out.println(str.substring(1,3)+str.substring(4,5)+
str.substring(6,8));
}
}
Which of the following option is the output of the above program?
a. java
b. npteljava
c. nptel java
d. nptel
Correct Answer: d
Detailed Solution:
The output can be checked by execution.
QUESTION 10:
What is the output of the following program?
public class Main{
public void static main(String args[]){
char a = '3';
int b = 011;
System.out.println(a+b);
}
}
a. 60
b. 3011
c. 33
d. Compilation error
Correct Answer: d
Detailed Solution:
There is a compilation error due to incorrect syntax in the statement public void static
main(String args[]); it should be public static void main(String args[]){
******