100+ Java Programming MCQs

Java Questions & Answers – Class Fundamentals & Declaring objects

1. What is the stored in the object obj in following lines of Java code?

   box obj;

a) Memory address of allocated memory of object
b) NULL
c) Any arbitrary pointer
d) Garbage
Answer: b
2. Which of these keywords is used to make a class?
a) class
b) struct
c) int
d) none of the mentioned
Answer: a
3. Which of the following is a valid declaration of an object of class Box?
a) Box obj = new Box();
b) Box obj = new Box;
c) obj = new Box();
d) new Box obj;
Answer: a
4. Which of these operators is used to allocate memory for an object?
a) malloc
b) alloc
c) new
d) give
Answer: c
5. Which of these statement is incorrect?
a) Every class must contain a main() method
b) Applets do not require a main() method at all
c) There can be only one main() method in a program
d) main() method must be made public
Answer: a
6. What will be the output of the following Java program?

    class main_class
    {
        public static void main(String args[])
        {
            int x = 9;
            if (x == 9)
            {
                int x = 8;
                System.out.println(x);
            }
        }
    }

a) 9
b) 8
c) Compilation error
d) Runtime error
Answer: c
7. Which of the following statements is correct?
a) Public method is accessible to all other classes in the hierarchy
b) Public method is accessible only to subclasses of its parent class
c) Public method can only be called by object of its class
d) Public method can be accessed by calling object of the public class
Answer: a
8. What will be the output of the following Java program?

    class box
    {
        int width;
        int height;
        int length;
    }
    class mainclass
    {
        public static void main(String args[])
        {
             box obj = new box();
             obj.width = 10;
             obj.height = 2;
             obj.length = 10;
             int y = obj.width * obj.height * obj.length;
             System.out.print(y);
        }
    }

a) 12
b) 200
c) 400
d) 100
Answer: b
9. What will be the output of the following Java program?

    class box
    {
        int width;
        int height;
        int length;
    }
    class mainclass
    {
        public static void main(String args[])
        {
            box obj1 = new box();
            box obj2 = new box();
            obj1.height = 1;
            obj1.length = 2;
            obj1.width = 1;
            obj2 = obj1;
            System.out.println(obj2.height);
        }
    }

a) 1
b) 2
c) Runtime error
d) Garbage value
Answer: a
10. What will be the output of the following Java program?

    class box
   {
        int width;
        int height;
        int length;
   }
    class mainclass
    {
        public static void main(String args[])
        {
            box obj = new box();
            System.out.println(obj);
        }
    }

a) 0
b) 1
c) Runtime error
d) [email protected] in hexadecimal form
Answer: d

Java Questions & Answers – Introduction To Methods

1. What is the return type of a method that does not return any value?
a) int
b) float
c) void
d) double
Answer: c
2. What is the process of defining more than one method in a class differentiated by method signature?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned
Answer: b
3. Which of the following is a method having same name as that of it’s class?
a) finalize
b) delete
c) class
d) constructor
Answer: d
4. Which method can be defined only once in a program?
a) main method
b) finalize method
c) static method
d) private method
Answer: a
5. Which of this statement is incorrect?
a) All object of a class are allotted memory for the all the variables defined in the class
b) If a function is defined public it can be accessed by object of other class by inheritation
c) main() method must be made public
d) All object of a class are allotted memory for the methods defined in the class
Answer: d
6. What will be the output of the following Java program?

    class box
    {
        int width;
        int height;
        int length;
        int volume;
        void volume(int height, int length, int width)
        {
             volume = width*height*length;
        }
    }
    class Prameterized_method
    {
        public static void main(String args[])
        {
            box obj = new box();
            obj.height = 1;
            obj.length = 5;
            obj.width = 5;
            obj.volume(3,2,1);
            System.out.println(obj.volume);
        }
     }

a) 0
b) 1
c) 6
d) 25
Answer: c
7. What will be the output of the following Java program?

    class equality
    {
        int x;
        int y;
        boolean isequal()
        {
            return(x == y);
        }
    }
    class Output
    {
        public static void main(String args[])
        {
            equality obj = new equality();
            obj.x = 5;
            obj.y = 5;
            System.out.println(obj.isequal());
        }
    }

a) false
b) true
c) 0
d) 1
Answer: b
8. What will be the output of the following Java program?

    class box
    {
        int width;
        int height;
        int length;
        int volume;
        void volume()
        {
             volume = width*height*length;
        }
    }
    class Output
    {
        public static void main(String args[])
        {
            box obj = new box();
            obj.height = 1;
            obj.length = 5;
            obj.width = 5;
            obj.volume();
            System.out.println(obj.volume);
        }
    }

a) 0
b) 1
c) 25
d) 26
Answer: c
9. In the following Java code, which call to sum() method is appropriate?

class Output
{

        public static int sum(int ...x)
        {
             return;
        }
        static void main(String args[])
        {
             sum(10);
             sum(10,20);
             sum(10,20,30);
             sum(10,20,30,40);
        }
}

a) only sum(10)
b) only sum(10,20)
c) only sum(10) & sum(10,20)
d) all of the mentioned
Answer: d
10. What will be the output of the following Java program?

    class area
    {
        int width;
        int length;
        int volume;
        area()
        {
           width=5;
           length=6;
        }
        void volume()
        {
             volume = width*length*height;
        }
    }
    class cons_method
    {
        public static void main(String args[])
        {
            area obj = new area();
            obj.volume();
            System.out.println(obj.volume);
        }
    }

a) 0
b) 1
c) 30
d) error
Answer: d

Java Questions & Answers – Constructors & Garbage Collection

1. What is the return type of Constructors?
a) int
b) float
c) void
d) none of the mentioned
Answer: d
2. Which keyword is used by the method to refer to the object that invoked it?
a) import
b) catch
c) abstract
d) this
Answer: d
3. Which of the following is a method having same name as that of its class?
a) finalize
b) delete
c) class
d) constructor
Answer: d
4. Which operator is used by Java run time implementations to free the memory of an object when it is no longer needed?
a) delete
b) free
c) new
d) none of the mentioned
Answer: d
5. Which function is used to perform some action when the object is to be destroyed?
a) finalize()
b) delete()
c) main()
d) none of the mentioned
Answer: a
6. What will be the output of the following Java code?

    class box
    {
        int width;
        int height;
        int length;
        int volume;
        box()
        {
            width = 5;
            height = 5;
            length = 6;
        }
        void volume()
        {
             volume = width*height*length;
        }
    }
    class constructor_output
    {
        public static void main(String args[])
        {
            box obj = new box();
            obj.volume();
            System.out.println(obj.volume);
        }
   }

a) 100
b) 150
c) 200
d) 250
Answer: b
7. What will be the output of the following Java code?

class Pro
{
     Pro()throws IOException
     {

     }

}
class Gies extends Pro
{
     Gies()
     {

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

     }
}

a) compile time error
b) run time error
c) compile and runs fine
d) unreported exception java.io.IOException in default constructor
Answer: a
8. What will be the output of the following Java code?

    class box
    {
        int width;
        int height;
        int length;
        int volume;
        void finalize()
        {
            volume = width*height*length;
            System.out.println(volume);
        }
        protected void volume()
       {
            volume = width*height*length;
            System.out.println(volume);
       }
    }
    class Output
    {
        public static void main(String args[])
        {
            box obj = new box();
            obj.width=5;
            obj.height=5;
            obj.length=6;
            obj.volume();
        }
    }

a) 150
b) 200
c) Run time error
d) Compilation error
Answer: a
9. Which of the following statements are incorrect?
a) default constructor is called at the time of object declaration
b) constructor can be parameterized
c) finalize() method is called when a object goes out of scope and is no longer needed
d) finalize() method must be declared protected
Answer: c
10. What will be the output of the following Java code?

10. What will be the output of the following Java code?
    class area
    {
        int width;
        int length;
        int area;
        void area(int width, int length)
        {
            this.width = width;
            this.length = length;
        }

    }
    class Output
    {
        public static void main(String args[])
        {
            area obj = new area();
            obj.area(5 , 6);
            System.out.println(obj.length + " " + obj.width);
        }
    }

a) 0 0
b) 5 6
c) 6 5
d) 5 5
Answer: c

Java Questions & Answers – Constructor

1. What is true about private constructor?
a) Private constructor ensures only one instance of a class exist at any point of time
b) Private constructor ensures multiple instances of a class exist at any point of time
c) Private constructor eases the instantiation of a class
d) Private constructor allows creating objects in other classes
Answer: a
2. What would be the behaviour if this() and super() used in a method?
a) Runtime error
b) Throws exception
c) compile time error
d) Runs successfully
Answer: c
3. What is false about constructor?
a) Constructors cannot be synchronized in Java
b) Java does not provide default copy constructor
c) Constructor can have a return type
d) “this” and “super” can be used in a constructor
Answer: c
4. What is true about Class.getInstance()?
a) Class.getInstance calls the constructor
b) Class.getInstance is same as new operator
c) Class.getInstance needs to have matching constructor
d) Class.getInstance creates object if class does not have any constructor
Answer: d
5. What is true about constructor?
a) It can contain return type
b) It can take any number of parameters
c) It can have any non access modifiers
d) Constructor cannot throw an exception
Answer: b
6. Abstract class cannot have a constructor.
a) True
b) False
Answer: b
7. What is true about protected constructor?
a) Protected constructor can be called directly
b) Protected constructor can only be called using super()
c) Protected constructor can be used outside package
d) protected constructor can be instantiated even if child is in a different package
Answer: b
8. What is not the use of “this” keyword in Java?
a) Passing itself to another method
b) Calling another constructor in constructor chaining
c) Referring to the instance variable when local variable has the same name
d) Passing itself to method of the same class
Answer: d
9. What would be the behaviour if one parameterized constructor is explicitly defined?
a) Compilation error
b) Compilation succeeds
c) Runtime error
d) Compilation succeeds but at the time of creating object using default constructor, it throws compilation error
Answer: d
10. What would be behaviour if the constructor has a return type?
a) Compilation error
b) Runtime error
c) Compilation and runs successfully
d) Only String return type is allowed
Answer: a

Java Questions & Answers – Heap and Garbage Collection

1. Which of the following has the highest memory requirement?
a) Heap
b) Stack
c) JVM
d) Class
Answer: c
2. Where is a new object allocated memory?
a) Young space
b) Old space
c) Young or Old space depending on space availability
d) JVM
Answer: a
3. Which of the following is a garbage collection technique?
a) Cleanup model
b) Mark and sweep model
c) Space management model
d) Sweep model
Answer: b
4. What is -Xms and -Xmx while starting jvm?
a) Initial; Maximum memory
b) Maximum; Initial memory
c) Maximum memory
d) Initial memory
Answer: a
5. Which exception is thrown when java is out of memory?
a) MemoryFullException
b) MemoryOutOfBoundsException
c) OutOfMemoryError
d) MemoryError
Answer: c
6. How to get prints of shared object memory maps or heap memory maps for a given process?
a) jmap
b) memorymap
c) memorypath
d) jvmmap
Answer: a
7. What happens to the thread when garbage collection kicks off?
a) The thread continues its operation
b) Garbage collection cannot happen until the thread is running
c) The thread is paused while garbage collection runs
d) The thread and garbage collection do not interfere with each other
Answer: c
8. Which of the below is not a Java Profiler?
a) JVM
b) JConsole
c) JProfiler
d) Eclipse Profiler
Answer: a
9. Which of the below is not a memory leak solution?
a) Code changes
b) JVM parameter tuning
c) Process restart
d) GC parameter tuning
Answer: c
10. Garbage Collection can be controlled by a program?
a) True
b) False
Answer: b

Java Questions & Answers – Overloading Methods & Argument Passing

1. What is the process of defining two or more methods within same class that have same name but different parameters declaration?
a) method overloading
b) method overriding
c) method hiding
d) none of the mentioned
Answer: a
2. Which of these can be overloaded?
a) Methods
b) Constructors
c) All of the mentioned
d) None of the mentioned
Answer: c
3. Which of these is correct about passing an argument by call-by-value process?
a) Copy of argument is made into the formal parameter of the subroutine
b) Reference to original argument is passed to formal parameter of the subroutine
c) Copy of argument is made into the formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument
d) Reference to original argument is passed to formal parameter of the subroutine and changes made on parameters of subroutine have effect on original argument
Answer: a
4. What is the process of defining a method in terms of itself, that is a method that calls itself?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion
Answer: d
5. What will be the output of the following Java code?

class Pro
{
 public void m1 (int i,float f)
 {
  System.out.println(" int float method");
 }

 public void m1(float f,int i);
  {
  System.out.println("float int method");
  }

  public static void main(String[]args)
  {
    Pro s=new Pro();
        s.m1(20,20);
  }
}

a) int float method
b) float int method
c) compile time error
d) run time error
Answer: c
6. What will be the output of the following Java code?

    class overload
    {
        int x;
 	int y;
        void add(int a)
        {
            x =  a + 1;
        }
        void add(int a, int b)
        {
            x =  a + 2;
        }
    }
    class Overload_methods
    {
        public static void main(String args[])
        {
            overload obj = new overload();
            int a = 0;
            obj.add(6);
            System.out.println(obj.x);
        }
   }

a) 5
b) 6
c) 7
d) 8
Answer: c
7. What will be the output of the following Java code?

    class overload
    {
        int x;
 	int y;
        void add(int a)
        {
            x =  a + 1;
        }
        void add(int a , int b)
        {
            x =  a + 2;
        }
    }
    class Overload_methods
    {
        public static void main(String args[])
        {
            overload obj = new overload();
            int a = 0;
            obj.add(6, 7);
            System.out.println(obj.x);
        }
    }

a) 6
b) 7
c) 8
d) 9
Answer: c
8. What will be the output of the following Java code?

   class overload
   {
        int x;
 	double y;
        void add(int a , int b)
        {
            x = a + b;
        }
        void add(double c , double d)
        {
            y = c + d;
        }
        overload()
        {
            this.x = 0;
            this.y = 0;
        }
    }
    class Overload_methods
    {
        public static void main(String args[])
        {
            overload obj = new overload();
            int a = 2;
            double b = 3.2;
            obj.add(a, a);
            obj.add(b, b);
            System.out.println(obj.x + " " + obj.y);
        }
   }

a) 6 6
b) 6.4 6.4
c) 6.4 6
d) 4 6.4
Answer: d
9. What will be the output of the following Java code?

    class test
    {
        int a;
        int b;
        void meth(int i , int j)
        {
            i *= 2;
            j /= 2;
        }
    }
    class Output
    {
        public static void main(String args[])
        {
            test obj = new test();
	    int a = 10;
            int b = 20;
            obj.meth(a , b);
            System.out.println(a + " " + b);
        }
    }

a) 10 20
b) 20 10
c) 20 40
d) 40 20
Answer: a
10. What will be the output of the following Java code?

    class test
    {
        int a;
        int b;
        test(int i, int j)
        {
            a = i;
            b = j;
        }
        void meth(test o)
        {
            o.a *= 2;
            O.b /= 2;
        }
    }
    class Output
    {
        public static void main(String args[])
        {
            test obj = new test(10 , 20);
            obj.meth(obj);
            System.out.println(obj.a + " " + obj.b);
        }
    }

a) 10 20
b) 20 10
c) 20 40
d) 40 20
Answer: b

Java Questions & Answers – Access Control – 1

1. Which of these access specifiers must be used for main() method?
a) private
b) public
c) protected
d) none of the mentioned
Answer: b
2. Which of these is used to access a member of class before object of that class is created?
a) public
b) private
c) static
d) protected
Answer: c
3. Which of these is used as a default for a member of a class if no access specifier is used for it?
a) private
b) public
c) public, within its own package
d) protected
Answer: a
4. What is the process by which we can control what parts of a program can access the members of a class?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion
Answer: c
5. Which of the following statements are incorrect?
a) public members of class can be accessed by any code in the program
b) private members of class can only be accessed by other members of the class
c) private members of class can be inherited by a subclass, and become protected members in subclass
d) protected members of a class can be inherited by a subclass, and become private members of the subclass
Answer: c
6. What will be the output of the following Java code?

    class access
    {
        public int x;
 	private int y;
        void cal(int a, int b)
        {
            x =  a + 1;
            y =  b;
        }
    }
    public class access_specifier
    {
        public static void main(String args[])
        {
            access obj = new access();
            obj.cal(2, 3);
            System.out.println(obj.x + " " + obj.y);
        }
   }

a) 3 3
b) 2 3
c) Runtime Error
d) Compilation Error
Answer: c
7. What will be the output of the following Java code?

    class access
    {
        public int x;
 	private int y;
        void cal(int a, int b)
        {
            x =  a + 1;
            y =  b;
        }
        void print()
        {
            System.out.println(" " + y);
        }
    }
    public class access_specifier
    {
        public static void main(String args[])
        {
            access obj = new access();
            obj.cal(2, 3);
            System.out.println(obj.x);
            obj.print();
        }
   }

a) 2 3
b) 3 3
c) Runtime Error
d) Compilation Error
Answer: b
8. What will be the output of the following Java code?

    class static_out
    {
        static int x;
 	static int y;
        void add(int a, int b)
        {
            x = a + b;
            y = x + b;
        }
    }
    public class static_use
    {
        public static void main(String args[])
        {
            static_out obj1 = new static_out();
            static_out obj2 = new static_out();
            int a = 2;
            obj1.add(a, a + 1);
            obj2.add(5, a);
            System.out.println(obj1.x + " " + obj2.y);
        }
   }

a) 7 7.4
b) 6 6.4
c) 7 9
d) 9 7
Answer: c
9. Which of these access specifier must be used for class so that it can be inherited by another subclass?
a) public
b) private
c) protected
d) none of the mentioned
Answer: a

Java Questions & Answers – Access Control – 2

1. Which one of the following is not an access modifier?
a) Public
b) Private
c) Protected
d) Void
Answer: d
2. All the variables of class should be ideally declared as?
a) private
b) public
c) protected
d) default
Answer: a
3. Which of the following modifier means a particular variable cannot be accessed within the package?
a) private
b) public
c) protected
d) default
Answer: a
4. How can a protected modifier be accessed?
a) accessible only within the class
b) accessible only within package
c) accessible within package and outside the package but through inheritance only
d) accessible by all
Answer: c
5. What happens if constructor of class A is made private?
a) Any class can instantiate objects of class A
b) Objects of class A can be instantiated only within the class where it is declared
c) Inherited class can instantiate objects of class A
d) classes within the same package as class A can instantiate objects of class A
Answer: b
6. All the variables of interface should be?
a) default and final
b) default and static
c) public, static and final
d) protect, static and final
Answer: c
7. What is true of final class?
a) Final class cause compilation failure
b) Final class cannot be instantiated
c) Final class cause runtime failure
d) Final class cannot be inherited
Answer: d
8. How many copies of static and class variables are created when 10 objects are created of a class?
a) 1, 10
b) 10, 10
c) 10, 1
d) 1, 1
Answer: a
9. Can a class be declared with a protected modifier.
a) True
b) False
Answer: b
10. Which is the modifier when there is none mentioned explicitly?
a) protected
b) private
c) public
d) default
Answer: d

Java Questions & Answers – Arrays Revisited & Keyword Static

1. Arrays in Java are implemented as?
a) class
b) object
c) variable
d) none of the mentioned
Answer: b
2. Which of these keywords is used to prevent content of a variable from being modified?
a) final
b) last
c) constant
d) static
Answer: a
3. Which of these cannot be declared static?
a) class
b) object
c) variable
d) method
Answer: b
4. Which of the following statements are incorrect?
a) static methods can call other static methods only
b) static methods must only access static data
c) static methods can not refer to this or super in any way
d) when object of class is declared, each object contains its own copy of static variables
Answer: d
5. Which of the following statements are incorrect?
a) Variables declared as final occupy memory
b) final variable must be initialized at the time of declaration
c) Arrays in java are implemented as an object
d) All arrays contain an attribute-length which contains the number of elements stored in the array
Answer: a
6. Which of these methods must be made static?
a) main()
b) delete()
c) run()
d) finalize()
Answer: a
7. What will be the output of the following Java program?

    class access
    {
        public int x;
 	static int y;
        void cal(int a, int b)
        {
            x +=  a ;
            y +=  b;
        }
    }
    class static_specifier
    {
        public static void main(String args[])
        {
            access obj1 = new access();
            access obj2 = new access();
            obj1.x = 0;
            obj1.y = 0;
            obj1.cal(1, 2);
            obj2.x = 0;
            obj2.cal(2, 3);
            System.out.println(obj1.x + " " + obj2.y);
        }
   }

a) 1 2
b) 2 3
c) 3 2
d) 1 5
Answer: d
8. What will be the output of the following Java program?

    class access
    {
       static int x;
       void increment()
       {
           x++;
       }
     }
    class static_use
    {
        public static void main(String args[])
        {
            access obj1 = new access();
            access obj2 = new access();
            obj1.x = 0;
            obj1.increment();
            obj2.increment();
            System.out.println(obj1.x + " " + obj2.x);
         }
   }

a) 1 2
b) 1 1
c) 2 2
d) Compilation Error
Answer: c
9. What will be the output of the following Java program?

    class static_out
    {
        static int x;
 	static int y;
        void add(int a , int b)
        {
            x = a + b;
            y = x + b;
        }
    }
    class static_use
    {
        public static void main(String args[])
        {
            static_out obj1 = new static_out();
            static_out obj2 = new static_out();
            int a = 2;
            obj1.add(a, a + 1);
            obj2.add(5, a);
            System.out.println(obj1.x + " " + obj2.y);
        }
   }

a) 7 7
b) 6 6
c) 7 9
d) 9 7
Answer: c
10. What will be the output of the following Java program?

    class Output
    {
        public static void main(String args[])
        {
            int arr[] = {1, 2, 3, 4, 5};
            for ( int i = 0; i < arr.length - 2; ++i)
                System.out.println(arr[i] + " ");
        }
    }

a) 1 2
b) 1 2 3
c) 1 2 3 4
d) 1 2 3 4 5
Answer: b
11. What will be the output of the following Java program?

    class Output
    {
        public static void main(String args[])
        {
            int a1[] = new int[10];
            int a2[] = {1, 2, 3, 4, 5};
            System.out.println(a1.length + " " + a2.length);
        }
    }

a) 10 5
b) 5 10
c) 0 10
d) 0 5
Answer: a

Java Questions & Answers – String Class

1. String in Java is a?
a) class
b) object
c) variable
d) character array
Answer: a
2. Which of these method of String class is used to obtain character at specified index?
a) char()
b) Charat()
c) charat()
d) charAt()
Answer: d
3. Which of these keywords is used to refer to member of base class from a subclass?
a) upper
b) super
c) this
d) none of the mentioned
Answer: b
4. Which of these method of String class can be used to test to strings for equality?
a) isequal()
b) isequals()
c) equal()
d) equals()
Answer: d
5. Which of the following statements are incorrect?
a) String is a class
b) Strings in java are mutable
c) Every string is an object of class String
d) Java defines a peer class of String, called StringBuffer, which allows string to be altered
Answer: b
6. What will be the output of the following Java program?

    class string_demo
    {
        public static void main(String args[])
        {
            String obj = "I" + "like" + "Java";
            System.out.println(obj);
        }
   }

a) I
b) like
c) Java
d) IlikeJava
Answer: d
7. What will be the output of the following Java program?

    class string_class
    {
        public static void main(String args[])
        {
            String obj = "I LIKE JAVA";
            System.out.println(obj.charAt(3));
        }
    }

a) I
b) L
c) K
d) E
Answer: a
8. What will be the output of the following Java program?

    class string_class
    {
        public static void main(String args[])
        {
            String obj = "I LIKE JAVA";
            System.out.println(obj.length());
        }
    }

a) 9
b) 10
c) 11
d) 12
Answer: c
9. What will be the output of the following Java program?

    class string_class
    {
        public static void main(String args[])
        {
            String obj = "hello";
            String obj1 = "world";
            String obj2 = obj;
            obj2 = " world";
            System.out.println(obj + " " + obj2);
        }
    }

a) hello hello
b) world world
c) hello world
d) world hello
Answer: c
10. What will be the output of the following Java program?

    class string_class
    {
        public static void main(String args[])
        {
            String obj = "hello";
            String obj1 = "world";
            String obj2 = "hello";
            System.out.println(obj.equals(obj1) + " " + obj.equals(obj2));
        }
    }

a) false false
b) true true
c) true false
d) false true
Answer: d

Java Questions & Answers – Methods Taking Parameters

1. Which of these is the method which is executed first before execution of any other thing takes place in a program?
a) main method
b) finalize method
c) static method
d) private method
Answer: c
2. What is the process of defining more than one method in a class differentiated by parameters?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned
Answer: b
3. Which of these can be used to differentiate two or more methods having the same name?
a) Parameters data type
b) Number of parameters
c) Return type of method
d) All of the mentioned
Answer: d
4. Which of these data type can be used for a method having a return statement in it?
a) void
b) int
c) float
d) both int and float
Answer: d
5. Which of these statement is incorrect?
a) Two or more methods with same name can be differentiated on the basis of their parameters data type
b) Two or more method having same name can be differentiated on basis of number of parameters
c) Any already defined method in java library can be defined again in the program with different data type of parameters
d) If a method is returning a value the calling statement must have a variable to store that value
Answer: d
6. What will be the output of the following Java program?

    class box
    {
        int width;
        int height;
        int length;
        int volume;
        void volume(int height, int length, int width)
        {
             volume = width * height * length;
        }
    }
    class Prameterized_method{
        public static void main(String args[])
        {
            box obj = new box();
            obj.height = 1;
            obj.length = 5;
            obj.width = 5;
            obj.volume(3, 2, 1);
            System.out.println(obj.volume);
        }
    }

a) 0
b) 1
c) 6
d) 25
Answer: c
7. What will be the output of the following Java program?

    class equality
    {
        int x;
        int y;
        boolean isequal()
        {
            return(x == y);
        }
    }
    class Output
    {
        public static void main(String args[])
        {
            equality obj = new equality();
            obj.x = 5;
            obj.y = 5;
            System.out.println(obj.isequal);
        }
    }

a) false
b) true
c) 0
d) 1
Answer: b
8. What will be the output of the following Java program?

    class box
    {
        int width;
        int height;
        int length;
        int volume;
        void volume()
        {
            volume = width * height * length;
        }
        void volume(int x)
        {
            volume = x;
        }
    }
    class Output
    {
        public static void main(String args[])
        {
            box obj = new box();
            obj.height = 1;
            obj.length = 5;
            obj.width = 5;
            obj.volume(5);
            System.out.println(obj.volume);
        }
    }

a) 0
b) 5
c) 25
d) 26
Answer: b
9. What will be the output of the following Java program?

    class Output
    {
        static void main(String args[])
        {
             int x , y = 1;
             x = 10;
             if(x != 10 && x / 0 == 0)
                 System.out.println(y);
             else
                 System.out.println(++y);
        }
    }

a) 1
b) 2
c) Runtime Error
d) Compilation Error
Answer: d
10. What will be the output of the following Java program?

    class area
    {
        int width;
        int length;
        int height;
        area()
        {
        width = 5;
        length = 6;
        height = 1;
        }
        void volume()
        {
             volume = width * height * length;
        }
    }
    class cons_method
    {
        public static void main(String args[])
        {
            area obj = new area();
            obj.volume();
            System.out.println(obj.volume);
        }
    }

a) 0
b) 1
c) 25
d) 30
Answer: d

Java Questions & Answers – Command Line Arguments – 1

1. Which of this method is given parameter via command line arguments?
a) main()
b) recursive() method
c) Any method
d) System defined methods
Answer: a
2. Which of these data types is used to store command line arguments?
a) Array
b) Stack
c) String
d) Integer
Answer: c
3. How many arguments can be passed to main()?
a) Infinite
b) Only 1
c) System Dependent
d) None of the mentioned
Answer: a
4. Which of these is a correct statement about args in the following line of code?

public static void main(String args[])

a) args is a String
b) args is a Character
c) args is an array of String
d) args in an array of Character
Answer: c
5. Can command line arguments be converted into int automatically if required?
a) Yes
b) No
c) Compiler Dependent
d) Only ASCII characters can be converted
Answer: b
6. What will be the output of the following Java program, Command line execution is done as – “java Output This is a command Line”?

    class Output
    {
        public static void main(String args[])
        {
            System.out.print(args[0]);
        }
    }

a) java
b) Output
c) This
d) is
Answer: c
7. What will be the output of the following Java program, Command line execution is done as – “java Output This is a command Line”?

    class Output
    {
        public static void main(String args[])
        {
            System.out.print(args[3]);
        }
    }

a) java
b) is
c) This
d) command
Answer: d
8. What will be the output of the following Java program, Command line execution is done as – java Output “This is a command Line”?

    class Output
    {
        public static void main(String args[])
        {
            for (String arg : args)
            {
                System.out.print(arg);
            }
        }
    }

a) This
b) java Output This is a command Line
c) This is a command Line
d) Compilation Error
Answer: c
9. What will be the output of the following Java program, Command line execution is done as – “java Output command Line 10 A b 4 N”?

    class Output
    {
        public static void main(String args[])
        {
            System.out.print((int)args[2] * 2);
        }
    }

a) java
b) 10
c) 20
d) error
Answer: d

10. What will be the output of the following Java program, Command line execution is done as – “java Output command Line 10 A b 4 N”?

    class Output
    {
        public static void main(String args[])
        {
            System.out.print(args[6]);
        }
    }

a) java
b) 10
c) b
d) N
Answer: d

Java Questions & Answers – Command Line Arguments – 2

1. What will be the output of the following Java snippet, if attempted to compile and run this code with command line argument “java abc Rakesh Sharma”?

public class abc
{
	int a=2000;
        public static void main(String argv[])
        {
	    System.out.println(argv[1]+" :-Please pay Rs."+a);
        }
}

a) Compile time error
b) Compilation but runtime error
c) Compilation and output Rakesh :-Please pay Rs.2000
d) Compilation and output Sharma :-Please pay Rs.2000
Answer: a
2. What will be the output of the following Java snippet, if attempted to compile and execute?

class abc
{
    public static void main(String args[])
    {
        if(args.length>0)
        System.out.println(args.length);
    }
}

a) The snippet compiles, runs and prints 0
b) The snippet compiles, runs and prints 1
c) The snippet does not compile
d) The snippet compiles and runs but does not print anything
Answer: d
3. What will be the output of the following Java snippet, if compiled and executed with command line argument “java abc 1 2 3”?

public class abc
{
   static public void main(String [] xyz)
   {
       for(int n=1;n<xyz.length; n++)
       {
          System.out.println(xyz[n]+"");
       }
   }
}

a) 1 2
b) 2 3
c) 1 2 3
d) Compilation error
Answer: b
4. What will be the output of the following Java code snippet running with “java demo I write java code”?

public class demo
{
   public static void main(String args[])
   {
        System.out.println(args[0]+""+args[args.length-1]);
   }
}

a) The snippet compiles, runs and prints “java demo”
b) The snippet compiles, runs and prints “java code”
c) The snippet compiles, runs and prints “demo code”
d) The snippet compiles, runs and prints “I code”
Answer: d
5. What will be the output of the following Java snippet, if compiled and executed with command line “hello there”?

public class abc
{
    String[] xyz;

    public static void main(String argv[])
    {
        xyz=argv;
    }

    public void runMethod()
    {
        System.out.println(argv[1]);
    }
}

a) Compile time error
b) Output would be “hello”
c) Output would be “there”
d) Output would be “hello there”
Answer: a
6. How do we pass command line argument in Eclipse?
a) Arguments tab
b) Variable tab
c) Cannot pass command line argument in eclipse
d) Environment variable tab
Answer: a
7. Which class allows parsing of command line arguments?
a) Args
b) JCommander
c) Command Line
d) Input
Answer: b
8. Which annotation is used to represent command line input and assigned to correct data type?
a) @Input
b) @Variable
c) @Command Line
d) @Parameter
Answer: d
9. What will be the output of the following Java code snippet run as $ java Demo –length 512 –breadth 2 -h 3?

class Demo {
    @Parameter(names={"--length"})
    int length;

    @Parameter(names={"--breadth"})
    int breadth;

    @Parameter(names={"--height","-h"})
    int height;

    public static void main(String args[])
    {
        Demo demo = new Demo();
        new JCommander(demo, args);
        demo.run();
    }

    public void run()
    {
        System.out.println(length+" "+ breadth+" "+height);
    }
}

a) 2 512 3
b) 2 2 3
c) 512 2 3
d) 512 512 3
Answer: c
10. What is the use of @syntax?
a) Allows multiple parameters to be passed
b) Allows one to put all your options into a file and pass this file as a parameter
c) Allows one to pass only one parameter
d) Allows one to pass one file containing only one parameter
Answer: b

Java Questions & Answers – Recursion

1. What is Recursion in Java?
a) Recursion is a class
b) Recursion is a process of defining a method that calls other methods repeatedly
c) Recursion is a process of defining a method that calls itself repeatedly
d) Recursion is a process of defining a method that calls other methods which in turn call again this method
Answer: c
2. Which of these data types is used by operating system to manage the Recursion in Java?
a) Array
b) Stack
c) Queue
d) Tree
Answer: b
3. Which of these will happen if recursive method does not have a base case?
a) An infinite loop occurs
b) System stops the program after some time
c) After 1000000 calls it will be automatically stopped
d) None of the mentioned
Answer: a
4. Which of these is not a correct statement?
a) A recursive method must have a base case
b) Recursion always uses stack
c) Recursive methods are faster that programmers written loop to call the function repeatedly using a stack
d) Recursion is managed by Java Runtime environment
Answer: d
5. Which of these packages contains the exception Stack Overflow in Java?
a) java.lang
b) java.util
c) java.io
d) java.system
Answer: a
6. What will be the output of the following Java program?

    class recursion
    {
        int func (int n)
        {
            int result;
            result = func (n - 1);
            return result;
        }
    }
    class Output
    {
        public static void main(String args[])
        {
            recursion obj = new recursion() ;
            System.out.print(obj.func(12));
        }
    }

a) 0
b) 1
c) Compilation Error
d) Runtime Error
Answer: d
7. What will be the output of the following Java program?

    class recursion
    {
        int func (int n)
        {
            int result;
            if (n == 1)
                return 1;
            result = func (n - 1);
            return result;
        }
    }
    class Output
    {
        public static void main(String args[])
        {
            recursion obj = new recursion() ;
            System.out.print(obj.func(5));
        }
    }

a) 0
b) 1
c) 120
d) None of the mentioned
Answer: b
8. What will be the output of the following Java program?

    class recursion
    {
        int fact(int n)
        {
            int result;
            if (n == 1)
                return 1;
            result = fact(n - 1) * n;
            return result;
        }
    }
    class Output
    {
        public static void main(String args[])
        {
            recursion obj = new recursion() ;
            System.out.print(obj.fact(5));
        }
    }

a) 24
b) 30
c) 120
d) 720
Answer: c
9. What will be the output of the following Java program?

    class recursion
    {
        int fact(int n)
        {
            int result;
            if (n == 1)
                return 1;
            result = fact(n - 1) * n;
            return result;
        }
    }
    class Output
    {
        public static void main(String args[])
        {
            recursion obj = new recursion() ;
            System.out.print(obj.fact(1));
        }
    }

a) 1
b) 30
c) 120
d) Runtime Error
Answer: a
10. What will be the output of the following Java program?

    class recursion
    {
        int fact(int n)
        {
            int result;
            if (n == 1)
                return 1;
            result = fact(n - 1) * n;
            return result;
        }
    }
    class Output
    {
        public static void main(String args[])
        {
            recursion obj = new recursion() ;
            System.out.print(obj.fact(6));
        }
    }

a) 1
b) 30
c) 120
d) 720
Answer: d



The content uploaded on this website is for reference purposes only. Please do it yourself first.