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 program?

class Test{ 
        public int display(int x, int y){ 
                return ("The sum of x and y is " + x + y); 
        } 

        public static void main(String args[]){ 
                Test test = new Test();
                System.out.println(test.display(4,5)); 
        } 
}

Answer: Option C

Explanation:

Return type of method display is int and we are trying to return string.

 

2.
interface Test{
      int p = 10; //line 1
      public int q = 20; //line 2
      public static int r = 30; //line 3
      public static final int s = 40; //line 4
}

Which of the above line will give compilation error? 

Answer: Option E

Explanation:

3.

The following program:

public class Test{ 
        static boolean isOK;
        public static void main(String args[]){
                System.out.print(isOK);
        } 
}

 

Answer: Option B

Explanation:

4.

What will be the output?

public class Test{
	static{
		int a = 5;
	}

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

	void call(){
		this.a++;
		System.out.print(this.a);
	}
}

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

Answer: Option A

Explanation:

5.

What will be the output after compiling and running following program code?

public class Test{
      static int a;
      public static void main(String[] args){
            System.out.println("one");
            call(1);
      }

      static void call(int a){
            this.a=10;
            System.out.println("two "+a);
      }
}

Answer: Option D

Explanation:

Static members are common for all objects, where as ‘this’ refer to a particular object. so we cant use ‘this’ operator in the static methods.

6.

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.

7.

What will be the output for the below code ?

public interface TestInf{
      int i =10;
}

public class Test{
      public static void main(String... args){
            TestInf.i=12;
	    System.out.println(TestInf.i);
      }
}

Answer: Option A

Explanation:

All the variables declared in interface is implicitly static and final , therefore can't change the value.

8.

Consider the following two classes declared and defined in two different packages, what can be added in class B to form what considered a correct access to class A from main() method of class B?

package subPackage;
public class A { }


package anotherPackage;
// line 1
	public class B{
		public static void main(String[] args){
		// line 2
	}
}

 1. At line1 add noting; At line2 add: new A();
2. At line 1 add: import package.*; at line 2 add : new subPackage.A();
3. At line 1 add: import subPackage.*; at line 2 add : new A();
4. At line 1 add: import subPackage.A; at line 2 add : new A();

Answer: Option C

Explanation:

9.

What is the output for the below code ?

class A{
      int k;
      boolean istrue;
      static int p;
      public void printValue(){
            System.out.print(k);
            System.out.print(istrue);
            System.out.print(p);
      }
}

public class Test{
      public static void main(String argv[]){
            A a = new A();
            a.printValue();
      }
}

 

 

Answer: Option A

Explanation:

Global and static variable need not be initialized before use. Default value of global and static int variable is zero. Default value of boolean variable is false. Remember local variable must be initialized before use.

10.

Determine Output:

class A{
	public static void method(int i){
		System.out.print("Method 1");
	}

	public static int method(String str){
		System.out.print("Method 2");
		return 0;
	}
}

public class Test{
     
	public static void main(String args[]){
		A.method(5);
	}
}

Answer: Option A

Explanation:

Overloading depends only on the method or constructor name and its parameter list i.e.
         method(int i) and method(String str)

So, overloading does not depend on access modifiers(public, private, protected), access specifiers(static, final) and return type.

 

11.

What is the output for the below code ?

interface A{
      public void printValue();
}

1. public class Test{
2.       public static void main (String[] args){
3.             A a1 = new A(){
4.                          public void printValue(){
5.                                System.out.println("A");
6.                          }
7.                    };
8.             a1.printValue();
9.       }
10. }

Answer: Option B

Explanation:

The A a1 reference variable refers not to an instance of interface A, but to an instance of an anonymous (unnamed) class. So there is no compilation error.

12.

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

public class Test{
        public static void main(String[] args){
                int[] a = new int[0];
                System.out.print(a.length);
        }
}

Answer: Option A

Explanation:

13.

What will be the output of the program?

public class Test{
      public static void main(String [] args){
            String s1 = args[1];
            String s2 = args[2];
            String s3 = args[3];
            String s4 = args[4];
            System.out.print(" args[2] = " + s2);
      }
}

 and the command-line invocation is C:Java> java Test 1 2 3 4

Answer: Option D

Explanation:

An exception is thrown because in the code String s4 = args[4];, the array index (the fifth element) is out of the bounds. The exception thrown is the ArrayIndexOutOfBoundsException.

14.
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:

15.

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: