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.

Determine output of the following program code?

public class Test{
      public static void main(String args[]){
            int i;
            try{
                  i = calculate();
                  System.out.println(i);
            }catch(Exception e){
                  System.out.println("Error occured");
            }
      }

      static int calculate(){
            return (7/2);
      }
}

Answer: Option A

Explanation:

2.

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.

3.

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

4.

What will be the output?

public class Test{
        public static void main(String[] args){
                int[] a = new int[4];
                a[1] = 1;
                a = new int[2];
                System.out.println("a[1] is " + a[1]);
        }
}

Answer: Option C

Explanation:

After executing the statement a = new int[2], a refers to int[2]. The default value for a[0] and a[1] is 0.

5.

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.

6.

Determine output:

class A{
      public void printValue(){
            System.out.println("Value-A");
      }
}
class B extends A{
      public void printNameB(){
            System.out.println("Name-B");
      }
}
class C extends A{
      public void printNameC(){
            System.out.println("Name-C");
      }
}

1. public class Test{
2.       public static void main (String[] args){
3.             B b = new B();
4.             C c = new C();
5.             newPrint(b);
6.             newPrint(c);
7.       }
8.       public static void newPrint(A a){
9.             a.printValue();
10.      }
11. }

Answer: Option B

Explanation:

Class B extended Class A therefore all methods of Class A will be available to class B except private methods. Class C extended Class A therefore all methods of Class A will be available to class C except private methods.

7.

Determine output:

public class Test{
      public static void main(String args[]){
            MyClass obj = new MyClass();
            obj.val = 1;
            obj.call(obj);
            System.out.println(obj.val);
      }
}

class MyClass{
      public int val;
      public void call(MyClass ref){
            ref.val++;
      }
}

Answer: Option B

Explanation:

8.

Choose the correct statement

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

Answer: Option B

Explanation:

9.

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:

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.
class A{
      A(String s){}

      A(){}
}

1. class B extends A{
2.       B(){}
3.       B(String s){
4.             super(s);
5.       }
6.       void test(){
7.             // insert code here
8.       }
9. }

Which of the below code can be insert at line 7 to make clean compilation ? 

Answer: Option A

Explanation:

12.

Determine Output:

class MyClass{
      static final int a = 20;

      static final void call(){
            System.out.println("two");
      }
	
      static{
            System.out.println("one");
      }
}

public class Test{
      public static void main(String args[]){
            System.out.println(MyClass.a);
      }
}

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

Answer: Option D

Explanation:

13.

What will be the output for the below code?

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

      public static void main(String[] args){
            System.out.println(a);
      }
}

Answer: Option A

Explanation:

A variable declared in a static initializer is not accessible outside its enclosing block.

14.

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.

15.

Given the code. What is the result when this program is executed?

public class Test{
      static int x[];
    
      static{
            x[0] = 1;
      }
    
      public static void main(String args[]){
      }
}

Answer: Option B

Explanation: