11.
package practice;
public class Tester {
void display(){
int arr[][]=new int[][5];
}
public static void main(String []args){
Tester t=new Tester();
t.display();
}
}
Ans: Compilation Error
Explanation: In the line int arr[][]=new int[][5], the second dimension cannot be specified after an empty array dimension.
12.
package tpg;
public class Tester {
public static void main(String []args){
Tester t=new Tester();
t.display();
}
public void display(){
int arr[][];
}
}
Ans: No error.
Explanation: It is perfectly legal to declare an array without declaring its size.
13.
package tpg;
public class Tester {
public static void main(String []args){
Tester t=new Tester();
t.display();
}
public void display(){
int arr[]=new int[5];
for(int i=0;i<arr.length;i++)
{
System.out.println("Element value: "+arr[5]);
}
}
}
Ans: ArrayIndexOutOfBoundsException occurs at runtime.
Explanation: For arrays in Java, index begins at 0.
14.
package tpg;
interface A{
void k();
}
class Tester implements A{
void k(){
System.out.println("Java is fun");
}
}
Ans: Compilation error
Explanation: All methods in an interface are by default public abstract void. But while over-riding in the class Tester, we are attempting to assign weaker access privilege in class Tester(default or package level access if not mentioned). Implementation in the class Tester should be done using the word public.
15.
package tpg;
public class Tester {
public static void main(String[] args) {
Tester t = new Tester();
t.display();
}
public void display() {
int i = 1;
if (i) {
System.out.println("In IF condition");
} else {
System.out.println("In ELSE condition");
}
}
}
Ans: Compilation error
Explanation: IF condition in Java accepts only Boolean values and not integers like C Language. The logic would have worked if the program was written in C language.
No comments:
Post a Comment