PROGRAMMING IN JAVA Assignment 2 | NPTEL | Answer with Explanation
Test Your Java Fundamentals with a Challenging MCQ Assignment on " Java Programming Elements" Sharpen your core Java skills with this engaging multiple-choice quiz (MCQ). Designed for beginners, this Assignment 2 assesses your understanding of fundamental Java concepts. Whether you're new to programming or refreshing your knowledge, this MCQ [Java Programming Elements ] challenge is a perfect way to gauge your progress and identify areas for improvement.
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. 22221010b. 12222101
c. 22101010
d. 22221012
Answer
a. 22221010
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.
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.
Answer
a. Reference of the array.
Reference of the array is passed to a method in Java.
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. 28b. -29
c. 30
d. -31
Answer
c. 30
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++.
How many bits are needed for float and double in Java, respectively?
a. 32 and 64b. 32 and 32
c. 64 and 64
d. 64 and 32
Answer
a. 32 and 64
Float needs 4 bytes or 32 bits and Double needs 8 bytes or 64 bits.
Which of the following is a valid automatic type conversion in Java?
a. short to byteb. float to long
c. int to short
d. int to long
Answer
d. int to long
Automatic type conversation is possible in the following sequence (reverse is not possible):
byte → short → int → long → float → double.
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
Answer
d. Compilation error
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);
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();
c. obj = new Student();
d. new Student obj;
Answer
b. Student obj = new Student();
When an object is to be declared it needs to be instantiated, hence the correct syntax of declaring
an object is (b).
| Learn Java Programming for free with Computer Course - CompEduBox Android app. Share with your Friends
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
Answer
c. 012
The output after execution is 012.
Consider the following piece of code.
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));
}
}
a. java
b. npteljava
c. nptel java
d. nptel
Answer
d. nptel
The output can be checked by execution.
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
Answer
d. Compilation error
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[]){
0 Comments