Home »
Java Aptitude Que. & Ans.
Java Basic Input & Output Aptitude Questions and Answers
This section contains Aptitude Questions and Answers on Java Basic Input Output, Declaration etc.
List of Java Basic I/O Aptitude Questions
1)
What will be the output of following program ?
public class Prg {
public static void main(String args[]){
System.out.print('A' + 'B');
}
}
- AB
- 195
- 131
- Error
Correct Answer: 3
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.
2)
What will be the output of following program ?
public class Prg {
public static void main(String args[]){
System.out.print("A" + "B" + 'A');
}
}
- ABA
- AB65
- Error
- AB
Correct Answer: 1
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".
3)
What will be the output of following program?
public class Prg {
public static void main(String args[]){
System.out.print(20+ 1.34f + "A" + "B");
}
}
- 201.34AB
- 201.34fAB
- 21.34AB
- Error
Correct Answer: 3
21.34AB
20 and 1.34f will be added and then 21.34 will be concatenated with “A” and “B”, hence output will be 21.34AB.
4)
What will be the output of following program?
public class Prg {
public static void main(String[] args) {
char [] str={'i','n','c','l','u','d','e','h','e','l','p'};
System.out.println(str.toString());
}
}
- includehelp
- Error
- [C@19e0bfd (Memory Address)
- NULL
Correct Answer: 3
[C@19e0bfd (Meory Address)
str is a character array, if you try to print str.toString() it will not converted to string because str is an object of character array that will print an address in string format.
5)
What will be the output of following program?
public class prg {
public static void main(String[] args) {
System.out.print("Hello");
System.out.println("Guys!");
}
}
- HelloGuys!
- Hello Guys!
- Hello
Guys!
- Compile with a Warning
Correct Answer: 1
HelloGuys!
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 HelloGuys! and then new line.