Friday, April 10, 2020

Java Programming - 1.3 IO Operations


1. Which of these class is not related to input and output stream in terms of functioning?
A. File
B. Writer
C. InputStream
D. Reader
Answer: Option A
Explanation: A File describes properties of a file, a File object is used to obtain or manipulate the information associated with a disk file, such as the permissions, time date, and directories path, and to navigate sub directories.


2. What will be the output of following program ?
class LWD{
public static void main(String[] args){
System.out.print("Keep on");
System.out.println("Learning");
}
}
A. Keep onLearning
B. Keep on Learning
C. Keep on
       Learning
D. Compile time error.
Answer & Explanation: A
Keep onLearning
System.out.print() does not print new line after printing string, while System.out.println(); prints new line after printing string. Hence output will be Keep onLearning and then new line.


3. What will be the output of following program ?
class LWD{
public static void main(String args[]){
const int a=10;
System.out.println(a);
}
}
A. 10
B. a
C. Unprintable Character
D. Compile time error
Answer and Explanation: D
Complie time error: due to illegal start of expression. JAVA does not support the const keyword, instead of const, final keyword is used.

4. What will be the output of following program ?
class LWD{
public static void main(String[] args){
char a=0x47;//Unicode of 'G'
char b=0x44;//Unicode of 'D'
System.out.print(a+"" + b+"");
System.out.print("-");
System.out.print(a+b);
}
}
A. GD-GD
B. GD-139
C. GD-Error
D. G D-139
Answer & Explanation: B
GD-139
a+"" and b+"" will be converted into string, .toString() or +"" after variable or value converts value into the string and a+b will be added because they are not converted into string. Hence output will be GD-139.

5. What will be the output of following program ?
class LWD{
public static void main(String[] lwd){
int a,b,c,d;
a=Integer.parseInt(lwd[1]);
b=Integer.parseInt(lwd[2]);
c=Integer.parseInt(lwd[3]);
d=Integer.parseInt(lwd[4]);
System.out.println(a+b+c+d);
}
}
Output & Explanation:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at LWD.main(MCQ_5.java:4)

6. What will be the output of following program ?
public class MCQ_1{
    public static void main(String args[]){
        System.out.print('A' + 'B');
    }
}
A. AB
B. 195
C. 131
D. Error
Answer & Explanation: C
131
Here, ‘A’ and ‘B’ are not strings they are characters. ‘A’ and ‘B’ will not concatenate. The Unicode of the ‘A’ and ‘B’ will add. The Unicode of ‘A’ is 65 and ‘B’ is 66. Hence Output will be 131.

7. What will be the output of following program ?
public class MCQ_2{
    public static void main(String args[]){
        System.out.print("A" + "B" + 'A');
    }
}
A. ABA
B. AB65
C. Error
D. AB
Answer & Explanation: A
ABA
If you try to concatenate any type of data like integer, character, float with string value, the result will be a string. So 'A' will be concatenated with "AB" and answer will be "ABA".

8. Which is the correct declaration of a boolean variable? 
A. boolean isAdult='false';
B. boolean isAdult=0;
C. boolean isAdult="false";
D. boolean isAdult=false;

Answer and Explanation: D
boolean isAdult=false;A boolean variable can store only true or false.


No comments:

Post a Comment