Hi all,
I have prepared an understanding document with 50 questions on Core Java. I would be sharing the same in the next 10 posts.
Happy learning!!!
1.
package practice;
public class Tester {
int transient=1;
void display(){
System.out.println(transient);
}
}
Ans: Compilation error.
Explanation: The variable name transient is invalid as it is a keyword. Keywords cannot be used to declare variables in Java.
2.
package practice;
public class Tester {
void display(){
System.out.println("Output: "+'1'+'2'+"Wipro");
}
public static void main(String []args){
Tester t=new Tester();
t.display();
}
}
Ans: 12Wipro
Explanation: ‘+’ is a concatenation operator in this example.
3.
package practice;
public class Tester {
void display(){
String sample='1';
System.out.println("Output: "+'1'+'2'+"Wipro");
}
public static void main(String []args){
Tester t=new Tester();
t.display();
}
}
Ans: Compilation Error
Explanation: In the line String sample=’1’, type mismatch occurs. You cannot store a character in a variable declared as String type.
4.
public class Tester {
void display(){
char ch='1';
System.out.println("Value: "+ch);
}
public static void main(String []args){
Tester t=new Tester();
t.display();
}
}
Ans: 1
Explanation: N/A
5.
package practice;
public class Tester {
void display(){
int arr[]={'1','2','3','4','5'};
for(int count=0;count<arr.length();count++){
System.out.println("Value: "+arr[count]);
}
}
public static void main(String []args){
Tester t=new Tester();
t.display();
}
}
Ans: Compilation error
Explanation: length is an attribute used to find the size of array, but length() is an method in String class. The program would have compiled successfully if length was used instead of length() method.
No comments:
Post a Comment