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:

public class Test {
        static void test(float x){
                System.out.print("float");
        }

        static void test(double x){
                System.out.print("double");
        }

        public static void main(String[] args){
                test(99.9);
        }
}

Answer: Option B

Explanation:

floating-point numbers are by default of type double.
99.9 is a double not a float.
To print "float" cast 99.9 to (float)

2.

What is the output for the below code ?

class A{
      public A(){
            System.out.println("A");
      }
      public A(int i){
            this();
            System.out.println(i);
      }
}
class B extends A{
      public B(){
            System.out.println("B");
      }
      public B(int i){
            this();
            System.out.println(i+3);
      }
}
public class Test{
      public static void main (String[] args){
            new B(5);
      }
}

Answer: Option A

Explanation:

Constructor of class B call their superclass constructor of class A (public A()), which execute first, and that constructors can be overloaded. Then come to constructor of class B (public B (int i)).

 

3.

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:

4.

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.

5.
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.

 

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

8.

What will be the output?

public class Test{
        public static void main(String[] args){
                int[] x = new int[3];
                System.out.println("x[0] is " + x[0]);
        }
}

Answer: Option C

Explanation:

Program is syntactically correct, so no error.
In java, if the array is not initialized at the time of declaration and creation then all the elements of the array are initialized to 0 by default.

9.

Which statements are most accurate regarding the following classes?

class A{
        private int i;
        protected int j;
}

class B extends A{
        private int k;
        protected int m;
}

Answer: Option B

Explanation:

10.

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.

11.

What will be the output for the below code ?

1. public class Test{
2.       public static void main(String[] args){
3.             byte i = 128;
4.             System.out.println(i);
5.       }
6. }

Answer: Option C

Explanation:

byte can only hold up to 127. So compiler complain about possible loss of precision.
static byte MAX_VALUE − This is constant holding the maximum value a byte can have, 27-1.
static byte MIN_VALUE − This is constant holding the minimum value a byte can have, -27.

12.

What will be the output?

class MyClass{
      public String test(){
            try{
                  System.out.print("One");
                  return "";
            }
            finally{
                  System.out.print("Two");
            }
      }
}

public class Test{
      public static void main(String args[]){
            MyClass m =  new MyClass();
            m.test();
      }
}

Answer: Option C

Explanation:

Finally block will execute irrespective of return statement in try block.

13.

What is the output for the below code ?

1. public class A{
2.       int add(int i, int j){
3.             return i+j;
4.       }
5. }
6. public class B extends A{
7.       public static void main(String argv[]){
8.             short s = 9;
9.             System.out.println(add(s,6));
10.      }
11.}

Answer: Option B

Explanation:

Cannot make a static reference to the non-static method add(int, int) from the type A. The short s is autoboxed correctly, but the add() method cannot be invoked from a static method because add() method is not static.

 

14.

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.

15.

What is output of the program?

class Test{
        public void display(int x, double y){
                System.out.println(x+y);
        } 
        public double display(int p, double q){
                return (p+q);
        }
        public static void main(String args[]){
                Test test = new Test();
                test.display(4, 5.0);
                System.out.println(test.display(4, 5.0));
        }
}

Answer: Option C

Explanation: