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.

The following fraction of code

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

 

Answer: Option A

Explanation:

2.

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};

3.

Given the following piece of code:

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

which of the following statements is true? 

Answer: Option D

Explanation:

4.

Determine output:

class A{
	{
		System.out.print("b1 ");
	}
	public A(){
		System.out.print("b2 ");
	}
}

class B extends A{
	static{
		System.out.print("r1 ");
	}
	public B(){
		System.out.print("r2 ");
	}
	{
		System.out.print("r3 ");
	}
	static{
		System.out.print("r4 ");
	}
}

public class Test extends B{
	public static void main(String[] args){
		System.out.print("pre ");
		new Test();
		System.out.println("post ");
	}
}

Answer: Option C

Explanation:

All static blocks execute first then blocks and constructor.

Blocks and constructor executes (super class block then super class constructor, sub class block then sub class constructor).

Sequence for static blocks is super class first then sub class.

Sequence for blocks is super class first then sub class.

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.

What is the value of a[1] after the following code is executed?

int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];

 

Answer: Option B

Explanation:

when i = 0;
a[i] = a[(a[i]+3)%a.length] //a.length =5;
a[0] = a[(a[0]+3)%5];
a[0] = a[(0+3)%5] ; // 3
a[0] = a[3] = 1
when i = 1;
a[1]=a[(a[1]+3)%5];
a[1]=a[(2+3)%5];
a[1]=a[0];
a[1]=1;
Therefore a[1] is equal to 1

7.

What will be the output for the below code ?

1. public class Test{
2.       int i=8;
3.       int j=9;
4.       public static void main(String[] args){
5.             add();
6.       }
7.       public static void add(){
8.             int k = i+j;
9.             System.out.println(k);
10.      }
11. }

Answer: Option D

Explanation:

i and j are instance variable and attempting to access an instance variable from a static method. So Compilation fails.

8.
public class Test { }

What is the prototype of the default constructor?

Answer: Option D

Explanation:

Option A and B are wrong because they use the default access modifier and the access modifier for the class is public (remember, the default constructor has the same access modifier as the class).

Option D is wrong. The void makes the compiler think that this is a method specification - in fact if it were a method specification the compiler would spit it out.

 

9.

Given the following piece of code:

public interface Guard{
        void doYourJob();
}
abstract public class Dog implements Guard{ }

which of the following statements is correct?

Answer: Option D

Explanation:

10.

Predict the output:

public class Test{     
      public static void main(String args[]){
            try{
                  String arr[] = new String[10];
                  arr = null;
                  arr[0] = "one";
                  System.out.print(arr[0]);
            }catch(Exception ex){
                  System.out.print("exception");
            }catch(NullPointerException nex){
                  System.out.print("null pointer exception");
            }   
      }
}

Answer: Option D

Explanation:

11.

You have the following code in a file called Test.java

class Base{
      public static void main(String[] args){
            System.out.println("Hello");
      }
}
public class Test extends Base{}

What will happen if you try to compile and run this? 

Answer: Option D

Explanation:

This will compile and print "Hello".
The entry point for a standalone java program is the main method of the class that is being run. The java run-time system will look for that method in class Test and find that it should have such a method. It does not matter whether it is defined in the class itself or is inherited from a parent class.

12.

What will be the output?

1. public interface InfA{
2.       protected String getName();
3. }

public class Test implements InfA{
      public String getName(){
            return "test-name";
      }
      public static void main (String[] args){
            Test t = new Test();
            System.out.println(t.getName());
      }
}

Answer: Option B

Explanation:

Illegal modifier for the interface method InfA.getName(); only public and abstracts are permitted.

13.

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

14.

What will be the output when the following program is compiled and executed?

abstract class TestAbstract{
      String my_name;
      String myName(){
            my_name = "TestMirror";
            return my_name;
      }
      abstract void display();
}

public class Test extends TestAbstract{
      void display(){
            String n = myName();
	    System.out.print("My name is "+ n);
      }

      public static void main(String args[]){
            Test t = new Test();
	    t.display();
      }
}

Answer: Option A

Explanation:

The options B, C and D are incorrect options as in Java we can declare an abstract class comprising of abstract and non-abstract methods that will not lead to any compilation error. Therefore, option A is the correct answer implying that the 't' instance of Test class invokes the display method, which is implemented in the Test class. The display method invokes myName() method declared int the TestAbstract class and prints the name.

15.

What will be the output?

public class Test{
      public static void main(String[] args){
            String value = "abc";
            changeValue(value);
            System.out.println(value);
      }

      public static void changeValue(String a){
            a = "xyz";
      }
}

Answer: Option A

Explanation:

Java pass reference as value. passing the object reference, and not the actual object itself. Simply reassigning to the parameter used to pass the value into the method will do nothing, because the parameter is essentially a local variable.