Could anyone answer these q's.
Q. Cosider the code bellow,
import java.lang.*;
public class
Test {
void f() {
System.out.println("In Exception");
throw new RuntimeException("From f()");
}
void g() {
System.out.println("Before");
f();
System.out.println("After");
}
public static void main(
String[] args) {
Test s = new Test();
s.g();
}
class B{
int x;
B(){ x = 100;}
}
class A{
int x;
A(){x = 20;}
void m1(final B y){y.x = 10;}
void m(final B y){B x = y; x.x++; x = new B();}
public static void main(String arf[]){
A x = new A();
B b = new B();
System.out.println(x.x);
x.m1(b);
System.out.println(b.x);
x.m1(b);
System.out.println(b.x);
((A)x).m(b);
System.out.println(b.x);
}
}
a. will give Compiler error, becuase final can not be modified.
b. will give compiler error, because invalid final declaration
c. will give runtime error, because final gets modified.
d. Compile and runs fine.
Q What is the output of this program
public class ShortCircuit {
static boolean test1(int val) {
System.out.print(" test1 called ");
return val < 1;
}
static boolean test2(int val) {
System.out.print(" test2 called ");
return val < 2;
}
static boolean test3(int val) {
System.out.print(" test3 called ");
return val < 3;
}
public static void main(String[] args) {
if(test1(0) && test2(2) && test3(2))
System.out.println(" 1st expression is true");
else
System.out.println(" 1st expression is false");
if(test3(2) | | test2(2) | | test1(0))
System.out.println(" 2st expression is true");
else
System.out.println(" 2st expression is false");
}
}
Q. Consider the code
class A { static int i = 0;
public String toString() { return "Object A # " + i; }
A() { i++;}
}
public class VarArgs {
static void f(Object[] x) {
for(int i = 0; i < x.length; i++)
System.out.println(x[i]);
}
public static void main(String[] args) {
f(new Object[] {
new Integer(47),
new Float(3.14), new Double(11.11) });
f(new Object[] {"one", "two", "three" });
f(new Object[] {new A(), new A(), new A()});
}
}
a. This code will not compile
b. Compiles fine and throws run time exceptions.
c. Compiles & runs fine.
d. Compiles & runs but give no output
Q
public class Arrays1 {
public static void main(String[] args) {
int[] a1 = { 1, 2, 3, 4, 5 };
int a2[];
float f1[] = new float[5];
a2 = a1;
for(int i = 0; i < a2.length; i++)
f1[i] = (float)a2[i]++;
for(int i = 0; i < f1.length; i++)
System.out.println(
"a1[" + i + "] = " + f1[i]);
}
}
a. Program will give compiler error, because incompatable array type
b. output will be 0.0 0.0 0.0 0.0 0.0
c. output will be null null null null null
d. program will give complier error, because possible use of array before its
initialization.
e. will give 1.0 2.0 3.0 4.0 5.0
true or false
.
1. Integer value can be safely converted to a char with explicit casting.
2. char x = 092; will give compiler error.
i have one more doubt
i know that final methods may not be overidden but if that final method is declared private then can that method be used in subclass it will not be visible in the subclass.
Can private methods be overidden to be less private.