Log In Register

Units


Java Coding Questions - Questions and Answers

 

Practice these Java Coding Questions under Java Programming walk-in interview Questions/ examination questions with best tricks and short cuts with solution. Student (candidate) who want to crack the walk in interview, competitive exams and want to find short cuts and tricks to solve questions on Java Coding Questions for following purpose.


– Anyone who is wishing to increase their perfect knowledge of Java Coding Questions of Java Programming.
– Anyone who is preparing for aptitude test and increase his in depth aptitude knowledge Java Programming specifically Java Coding Questions.
– Anyone who is preparing for interviews (competitive examinations, govt examinations and bank examinations, off-campus or on campus interviews, walk-in interview and company interviews) having questions on Java Coding Questions under Java Programming.

– Anyone who is preparing for aptitude test or competitive exam or entrance exam.
– Anyone who is preparing for entrance examinations and other competitive examinations.
– All Experienced, Freshers and Students.

Here is list of Questions and Answers covering all key area of  Java Coding Questions topic of Java Programming:

 

1.

What is the result of compiling and running the following code?

class Base{
        private Base(){
                System.out.print("Base");
        }
}
public class test extends Base{
        public test(){
                System.out.print("Derived");
        }
        public static void main(String[] args){
                new test();
        }
}

Answer: Option D

Explanation:

Implicit super constructor Base is not visible, must explicitly invoke another constructor.

2.

What will be output of the following program code?

public class Test{
	public static void main(String[] a){
		short x = 10;
		x = x*5;
		System.out.print(x);
	}  
}

 

Answer: Option C

Explanation:

lossy conversion from int to short
x = x*5;
       ^
1 error

3.

Choose the correct statement

public class Circle{
      private double radius;  
      public Circle(double radius){
            radius = radius;
      }
}

Answer: Option B

Explanation:

4.

What is the output of the following program code?

public class Test{
        public static void main(String args[]){
                try{
                        int i;
                        return;
                }
                catch(Exception e){
                        System.out.print("inCatchBlock");
                }
                finally{
                        System.out.println("inFinallyBlock");
                }
        }
}

Answer: Option C

Explanation:

5.

What is the output for the below code?

public class Test{
      public static void printValue(int i, int j, int k){
            System.out.println("int");
      }

      public static void printValue(byte...b){
            System.out.println("long");
      }

      public static void main(String... args){
            byte b = 9;
            printValue(b,b,b);
      }
}

Answer: Option B

Explanation:

Primitive widening uses the smallest method argument possible. (For Example if you pass short value to a method but method with short argument is not available then compiler choose method with int argument). But in this case compiler will prefer the older style before it chooses the newer style, to keep existing code more robust. var-args method is looser than widen.

6.

Given the following piece of code:

class SalaryCalculationException extends Exception{}
class Person{
        public void calculateSalary() throws SalaryCalculationException{
                //...
                throw new SalaryCalculationException();
                //...
        }
}
class Company{
        public void paySalaries(){
                new Person().calculateSalary();
        }
}

Which of the following statements is correct?
1. This code will compile without any problems.
2. This code will compile if in method paySalaries() we return a boolean in stead of void.
3. This code will compile if we add a try-catch block in paySalaries().
4. This code will compile if we add throws SalaryCalculationException in the signature of method paySalaries(). 

Answer: Option D

Explanation:

7.

What would be the result of attempting to compile and run the following code?

public class Test{
      public static void main(String[] args){
            double[] x = new double[]{1, 2, 3};
            System.out.println("Value is " + x[1]);
      }
}

Answer: Option D

Explanation:

new double[]{1, 2, 3} is correct. This is the syntax I have not covered in this edition, but will be covered in the future edition. In this question, double[] x = new double[]{1, 2, 3} is equivalent to double[] x = {1, 2, 3};

8.

What will be output of following program?

public class Test{
      public static void main(String[] args){
            byte b=127;
            b++;
            b++;
            System.out.println(b);
      }
}

Answer: Option C

Explanation:

Range of byte data in java is -128 to 127. But byte data type in java is cyclic in nature.

9.

Given the following piece of code:

public class School{
        public abstract double numberOfStudent();
}

which of the following statements is true? 

Answer: Option D

Explanation:

10.
class MyClass{
      MyClass(){
            System.out.print("one");
      }
      public void myMethod(){
            this();
            System.out.print("two");
      }
}
 
public class TestClass{
      public static void main(String args[]){
            MyClass obj = new MyClass();
            obj.myMethod();
      }
}

Answer: Option D

Explanation:

A method can't have constructor call.

 

11.

What is the output of the following program?

public class Test{
	static int x  = 10 ;
       	public static void main(String[] a){
		 Test test = new Test( ) ; 
		 Test test1 = new Test( ) ;
		 test.x  +=  1 ;
		 System.out.println(  test.x + test1.x ) ;    
	}
}

 

Answer: Option C

Explanation:

Static variable have a single copy of memory. That means all the objects will share the same memory location. So, if the object test increase the value of x by 1, then object test1 will access that incremented value of x

12.

What will happen after compiling this program code?

abstract class MyClass{ //line 1
      private int a, b;

      public void call(int a, int b){
            this.a = a;
            this.b = b;
            System.out.print(a+b);
      }
}

public class Test{
      public static void main(String args[]){
            MyClass m = new MyClass(); //line 2
            m.call(12,25);
      }
}

Answer: Option C

Explanation:

Abstract class is not concrete class which means object cannot be created for abstract class, its requires extending it and then create the object of extended class.

13.

The following fraction of code

double STATIC = 2.5 ;
System.out.println( STATIC );

 

Answer: Option A

Explanation:

14.

What will be the output?

class A{
	static void method(){
		System.out.println("Class A method");
	}
}
class B extends A{
	static void method(){
		System.out.println("Class B method");
	}
}
public class Test{
	public static void main(String args[]){
		A a = new B();
		a.method();
	}
}

Answer: Option A

Explanation:

Overriding in Java simply means that the particular method would be called based on the run time type of the object and not on the compile time type. But in the above case the methods are static which means access to them is always resolved during compile time only using the compile time type information. Accessing them using object references is just an extra liberty given by the designers of Java.

15.

Determine output of the following code.

interface A { }

class C { }

class D extends C { }

class B extends D implements A { }

public class Test extends Thread{
        public static void main(String[] args){
                B b = new B();
                if (b instanceof A)
                        System.out.println("b is an instance of A");
                if (b instanceof C)
                        System.out.println("b is an instance of C");
        }
}

 

Answer: Option D

Explanation: