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 output of the following program?

class A{
        public static void main(String args[]){
	        byte b;
   	        int i = 258;
	        double d = 325.59;

	        b = (byte) i;
	        System.out.print(b);

	        i = (int) d;
	        System.out.print(i);

                b = (byte) d;
                System.out.print(b);
        }
}

Answer: Option C

Explanation:

In the type cast
b = (byte) i;
The value of i i.e 258 is cast into a byte variable. Whenever a casting done where source value is greater than the range of the destination then the result is the remainder of the division of source by the range of the destination. In this case the result is the remainder of the division of 258 by 256(the range of a byte), which is 2.

In the second type cast
i = (int) d;
the fractional component of d i

2.

What happens if the following program is compiled and executed?

interface MyInterface{
      void display();
}

interface MySubInterface extends MyInterface{
      void display();
}

public class Test implements MySubInterface{
      public void display(){
            System.out.print("Welcome to TestMirror.");
      }
      public static void main(String args[]){
            Test t = new Test();
	    t.display();
      }
}

Answer: Option C

Explanation:

The program will compile and execute successfully as you can declare methods with same name in an interface and the method of either interface can be used, implying the option a is incorrect. The option B and D are incorrect as the methods of an interface are implicitly public and abstract.

3.
public class MyClass{ }

For the above class(MyClass) what is the correct way of declaring constructor? 

Answer: Option D

Explanation:

Choice A,C are the correct answers.
The default access specifier for any constructor is same as the access specifier of class.so the options A and C are correct.

 

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.

Given the following piece of code:

class Person{
        public int number;
}
public class Test{
        public void doIt(int i , Person p){
                i = 5;
                p.number = 8;
        }
        public static void main(String args[]){
                int x = 0;
                Person p = new Person();
                new Test().doIt(x, p);
                System.out.println(x + " " + p.number);
        }
}

What is the result? 

Answer: Option A

Explanation:

6.

Determine output of the following program.

public class Test{
           public static void main(String args[]){
                  System.out.println( Math.floor( Math.random( ) ) ) ;
           }
}

Answer: Option C

Explanation:

random() is a static method defined in Math class.
Syntax: static double random()
random() always return a value in the range 0.0 <= Math.random() <1.0 So, taking floor function of any number between above range always yield 0.0.
Ex: Math.floor(0.958127101241...) = 0.0

    Math.floor(0.05684865565...) = 0.0
   

7.

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.

8.

What will be the result if NullPointerException occurs at line 2?

1.  try{
2.       //some code goes here
3.  }
4.  catch(NullPointerException ne){
5.       System.out.print("1 ");
6.  }
7.  catch(RuntimeException re){
8.       System.out.print("2 ");
9.  }
10. finally{
11.      System.out.print("3");
12. }

Answer: Option D

Explanation:

9.

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.

10.

What is the output for the below code?

public class Test{
      int _$;
      int $7;
      int do;

      public static void main(String argv[]){
            Test test = new Test();
            test.$7=7;
            test.do=9;
            System.out.println(test.$7);
            System.out.println(test.do);
            System.out.println(test._$);
      }
}

 

 

Answer: Option D

Explanation:

$7 is valid identifier. Identifiers must start with a letter, a currency character ($), or underscore ( _ ). Identifiers cannot start with a number. You can't use a Java keyword as an identifier. do is a Java keyword.

11.

Choose the correct statement

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

Answer: Option B

Explanation:

12.
class MyClass{
	int i;
	int j;

	public MyClass(int i, int j){
		this.i = i;
		this.j = j;
	}

	public void call(){
		System.out.print("One");
	}
}

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

Answer: Option B

Explanation:

13.

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:

14.

What can directly access and change the value of the variable qusNo?

package com.mypackage;

public class Test{
    private int qusNo = 100;
}

Answer: Option A

Explanation:

15.

What would be the output of the following fraction of code ?

int Integer = 34 ;
char String = 'S' ;
System.out.print( Integer ) ;
System.out.print( String ) ;

 

Answer: Option E

Explanation: