Single Post

Header

Monday, December 21, 2015

Java interview programs

Java interview programs

1.
public class Program1 {
public static void main(String args[]) {
int x=5;
boolean b1 = true ;
boolean b2 = false;
if ((x==4) && !b2)
   System.out.print("1 ");
System.out.print("2 ");
if((b2=true) && b1)
System.out.print("3");
}

}

Output:
2 3

2.
public class Program2 {
static String s1 = "java";
     static String s2 = "java";
 
public static void main(String[] args) {
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
}
}

Output:
true
true

3.
public class Program3 {
String s1 = "java";
     String s2 = "java";
public static void main(String[] args) {
System.out.print(s1==s2);
}
}

Output:Excption occurs
Cannot make a static reference to the non-static field s1
Cannot make a static reference to the non-static field s2

4.
public class Program4 {

public static void main(String[] args) {
String s1 = "java";
   String s2 = "java";
System.out.print(s1==s2);
}
}

Output:
true

5.
public class Program5 {
public static void main(String[]  args) {
String str1 = "123";
str1 += "10";
System.out.println(str1);
String str2 = "234";
str2.concat("10");
System.out.println(str2);
String str3 = "345";
str3=str3.concat("10");
System.out.println(str3);
}
}

Output:
12310
234
34510


6.
interface myInterface {
    int a= 8;
    void sample() ;
}

public class InterfaceA implements myInterface {
 //   a = 12;  syntax error
public void sample() {
System.out.println("test");
}
public static void main(String[]  args) {
InterfaceA obj = new InterfaceA();
obj.sample();
}

}

Output:
test

7.
 class A {
public void test() {
System.out.println("class A");
}
}

 class B extends A {
public void test() {
System.out.println("class B");
}
}

public class Overriding {

public static void main(String[] args) {
A a = new A();
a.test();
B b = new B();
b.test();
A a1 = new B();
a1.test();
// B b1 = new A(); cannot convert from A to B
    // b1.test();

}

}

Output:
class A
class B
class B


No comments:

Post a Comment