Welcome to "CoreJavaFundas"

These blog contains some simple but mind boggling core java questions which challenges your core java fundamentals. The questions were collected from various websites and presented in a simple way.I have deliberately not kept the answers so that you can get a chance to try.To get the answers you can always run the programs and check the outputs.Do post your comments if you like the blog or if you have some suggessions or queries...

Kuntal

Wednesday, April 14, 2010

core java "funda questions"

1.
int a2[4] = {3,4,5,6};
int a3[6];
int[] a4[] = new int [3][3];
int[] a5 = new int[] {2,3,5};
int[] a6 = new int[10];

2.
class C {
int i;
public static void main(String args[]){
private int a=1;
public int b=2;
protected int c=3;

System.out.println(a+b+c);
}}

3.
class C {
public static void main(String args[]){
System.out.println(args[1]);
System.out.println(args[2]);
System.out.println(args[3]);
}}

>java C a b c

4.
class C{
static int s;
public static void main(String a[]){
C obj=new C();
obj.m1();
System.out.println(s);
}
void m1();
{
int x=1;
m2(x);
System.out.println(x+"");
}
void m2(int x){
x=x*2;
s=x;
}}

5.
class C{
int i = 4;
public static void main(String args[]){

System.out.print(i);
}}

6.
class C {
public static void main(String[] args) {
int i1=1;
switch(i1){
case 1:
System.out.println("one");
case 2:
System.out.println("two");
case 3:
System.out.println("three");
}}}

7.
class C{
class A { void b() { System.out.println("Hii"); } }
public static void main(String[] args) {
A a = new A();
a.b();
}}

8.
class C1
{
static interface I
{
static class C2 {}
}
public static void main(String a[])
{
C1.I.C2 ob1=new C1.I.C2();
System.out.println("object created");
}}

9.
String s1 = "Hello";
String s2 = "World";
s1 = null; How many objects are eligible for GC here?
s1=s2; How many objects are eligible for GC here?
-----------------------------------------------------------
String s1 = new String("Hello");
String s2 = new String("World");
s1 = null; How many objects are eligible for GC here?
s1=s2; How many objects are eligible for GC here?
-----------------------------------------------------------
String s1 = "Hello";
String s2=s1;
String s3 = "World";
How many objects are created?

10.
class C{
public static void main(String a[])
{
C c1=new C();
C c2=m1(c1);
C c3=new C();
c2=c3;
How many objects are eligible for GC here?
}
static C m1(C ob1){
ob1 =new C();
return ob1;
}}

11.
class A{
{ System.out.println("1"); }
A()
{
System.out.println("2");
System.out.println("3");
}
static { System.out.println("4"); }
}

class B extends A{
{ System.out.println("5"); }
B()
{
System.out.println("6");
System.out.println("7");
}
{ System.out.println("8"); }
static { System.out.println("9"); }
}

class C extends B {
public static void main(String args[]){
System.out.println("10");
new C();
System.out.println("11");
}}

12.
class c1
{
public static void main(String a[])
{
c1 ob1=new c1();
Object ob2=ob1;
System.out.println(ob2 instanceof Object);
System.out.println(ob2 instanceof c1);
}
}

13.
class A extends Thread {
private int i;
public void run() {i = 1;}
public static void main(String[] args) {
A a = new A();
a.run();
System.out.print(a.i);
}}

14.
class parent
{
}
class child extends parent{
public static void main(String[] args) {
child[] c1 = new child[2];
parent[] p1 = new parent[2];
parent[] = p2;
child[] = c2;

p2=c1;
c2=p1;
}}

15.
class C{
public static void main (String[] args) {
byte b1=33;
b1++;
byte b2=55;
b2=b1+1;
System.out.println(b1+""+b2);
}}

16.
import java.util.*;
class C
{
final Vector v;
C()
{
v=new Vector();
}
C(int i)
{ }
public void someMethod()
{
System.out.println(v.isEmpty());
}
}

17.
class C1{
public void m1(){ }
}
class C2 extends C1{
private void m1(){ }
}

18.
interface I{
int i;
}
class C implements I{
public static void main(String a[]){
System.out.println(i);
System.out.println(i++);
}
}

19.
An abstract class must have at least one abstract method
(Is this statement true?)

20.
class C {
public static void main(String[] args) {
  boolean b1;
  b1=3<4<5;               
  System.out.println(b1); 
}}

21.
class C{
public static void main(String[] args) {
  try
    {
        int i1=3/0;
      }
    catch(Exception e)
    {
     System.out.println("exception1");
     }
    catch(NullPointerException e)
    {
    System.out.println("exception2");
        }
     finally
    {
              System.out.println("finally");

      }
}}

22.
class c1{
   void go(){}
}
class c2 extends c1
{
 String go()
{
  return null;
}
}

23.
class C {
public static void main(String[] args) {
   char  c1=65;
  switch(c1){
       case 'A':                             
               System.out.println("one");
        default:                             
               System.out.println("two");
      case 'b':                              
               System.out.println("three");
}}}

24.
class Base{
Base() { System.out.println("base"); }
}

class Super extends base {
Super() { System.out.println("super"); }

public static void main(String[] args) {
Super s = new Super(); }}

25.
class C{
 public static void main(String a[])      {
  int i1=9;
  int i2;
    if(i1>3) {        
        i2=8;
     }
   System.out.println(i2);
}}

26.
class A extends Thread {
public void run() {
System.out.print("A");
}
}
class B {
public static void main (String[] args) {
A a = new A();
a.start();
a.start(); // 1
}
}



27.
which one is true :
>Static methods can be OVERRIDDEN
>Static methods can be OVERLOADED




28.
class A{
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
int a1 = 1; long b1 = 2;
System.out.print(m(a1)+","+ m(b1));
}}


29.
When a byte is added to a char, what is the type of the result?


30.
class C{
public static void main(String args[]) {
int a = 1;
a += ++a + a++;
System.out.print(a);
}}
-------------------------------------------
class C{
public static void main(String args[]) {
int a = 1;
a += a++ + a++;
System.out.print(a);
}}

31.
interface I {
public class Inner {
Inner ( ) {
System .out . println ( "Inner Created" ) ;
}
};
};
will this give any compilation error?

32.
can we override the start() method of thread class?
if NO then why? if yes then what will happen?

33.
abstract class vehicle{
abstract public void speed();
}
class car extends vehicle{
public static void main (String args[]) {
vehicle ob1;
ob1=new car();
}}

34.
abstract class A {} // 1
transient class B {} // 2
private class C {} // 3
static class D {} // 4
Which of these declarations will produce a compile-time error?

35.
public static double sin(double angle)
What are the units of the "angle" argument?

Degrees
Radians
Both

36.
class base
{
base()
{System.out.println("base");}

base(int i1)
{System.out.println("has int");}

}
class Super extends base
{
Super()
{System.out.println("super");
super(1);}
public static void main(String [] a)
{
base b1=new Super();
}}

37.
class base
{
base()
{System.out.println("base");}

base(int i1)
{System.out.println("has int");}

}
class Super extends base
{
Super()
{super(1);
System.out.println("super"); }

public static void main(String [] a)
{ Super s = new Super();
base b1=new Super();
}}

38.
class C {
private String get ( String str ) {
try
{
throw new Exception ( ) ;
return str ;
}
catch ( Exception e )
{
return null ;
}
}
public static void main ( String peace [ ] ) {
try
{
System.out.println ( ( new C ( ) ).get ( " C " ) ) ;
}
catch ( Exception e )
{
System.out.println( "Exception" ) ;
}
}
};

39.
Which of the following methods are static members of the Thread class?
join
run
sleep
start
wait

40.
class C {
public static void main(String[] args) {
try
{
int i1=3/0;
}
System.out.println("hai");
catch(NullpointerException e)
{
}
finally
{
System.out.println("finally);

}
}}

41.
class C {
public static void main ( String ka [ ] ) {
Thread t = Thread . currentThread ( ) ;
t . setPriority ( - 1 ) ;
System . out . println ( " Done ! " ) ;
}
};

42.
class C {
static void m1(Object x) {System.out.print("Object");}
static void m1(String x) {System.out.print("String");}
public static void main(String[] args) {
m1(null);
}}

43.
class C {
public static void main(String[] args) {
int x=2;
int y=3;
if((y==++x)|(x<++y)){
System.out.println(x+""+y);
}
}}
-----------------------------------------
class C {
public static void main(String[] args) {
int x=2;
int y=3;
if((y==++x)||(x<++y)){
System.out.println(x+""+y);
}
}}

44.
can the returntype be different for overriding/overloading?
can the returntype of an overloaded method be an object of the type
which is a subtype of the class which is returntype for the mother method?
what is Covarient return?

45.
interface I{
final class C1 { //1
static int i=9;; //2
}
}
class C2 implements I{
public static void main(String a[]){
System.out.println(I.C1.i); ///3
}}

46.
class C {
public static void main ( String args[ ] ) {
while ( false ) {
System.out.println ( "Value" ) ;
}
}
}

47.
class C {
public static void main(String[] args) {
System.out.println(4+5+"String");
System.out.println("String"+4+5);
}}

48.
class H {
public static void main (String[] args) {
String s1 = "HHH";
StringBuffer sb2 = new StringBuffer(s1);
System.out.print(sb2.equals(s1) + "," + s1.equals(sb2));
}}

49.
class A extends Thread {
private int i;
public void run() {i = 1;}
public static void main(String[] args) {
A a = new A();
a.run();
System.out.print(a.i);
}}
How many threads are created in this Program and what are they?

50.
Is any subtype an instance of the maintype that is extended?

51.
which among these can an Outer class be
public
protected
private
static

52.
if(0.0 == -0.0) {
System.out.println("true");
}
else{
System.out.println("false");
}

53.
if("String".replace('t','T') == "String".replace('t','T'))
System.out.println("Equal");
else
System.out.println("Not Equal");

54.
can there be a recursive call to a constructor inside the same constructor?

55.
which are true statements:
An abstract class
-can have constructors
-must have one/more abstract methods
-cannot use this() because it cannot be instantiated
-cannot have super()

56.
interface A
{ public int foo(); }
interface B
{ public void foo(); }
class C implements A,B
{
public int foo() {}
public void foo() {}
public static void main(String args[]){
}}

57.
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(false);
Boolean b3 = new Boolean ("false");
Boolean b4 = new Boolean(b1);

System.out.println(b1.equals(b4));
System.out.println(b2==b3);
System.out.println(b1==b4);