50+ Java Programming MCQs

                     

1. Who invented Java Programming?
a) Guido van Rossum
b) James Gosling
c) Dennis Ritchie
d) Bjarne Stroustrup
Answer: b
2. Which statement is true about Java?
a) Java is a sequence-dependent programming language
b) Java is a code dependent programming language
c) Java is a platform-dependent programming language
d) Java is a platform-independent programming language
Answer: d
3. Which component is used to compile, debug and execute the java programs?
a) JRE
b) JIT
c) JDK
d) JVM
Answer: c
4. Which one of the following is not a Java feature?
a) Object-oriented
b) Use of pointers
c) Portable
d) Dynamic and Extensible
Answer: b
5. Which of these cannot be used for a variable name in Java?
a) identifier & keyword
b) identifier
c) keyword
d) none of the mentioned
Answer: c
6. What is the extension of java code files?
a) .js
b) .txt
c) .class
d) .java
Answer: d
7. What will be the output of the following Java code?

    class increment {
        public static void main(String args[]) 
        {        
             int g = 3;
             System.out.print(++g * 8);
        } 
    }

a) 32
b) 33
c) 24
d) 25
Answer: a
8. Which environment variable is used to set the java path?
a) MAVEN_Path
b) JavaPATH
c) JAVA
d) JAVA_HOME
Answer: d
9. What will be the output of the following Java program?

class output {
        public static void main(String args[]) 
        {
            double a, b,c;
            a = 3.0/0;
            b = 0/4.0;
            c=0/0.0;
 
	    System.out.println(a);
            System.out.println(b);
            System.out.println(c);
        } 
    }

a) NaN
b) Infinity
c) 0.0
d) all of the mentioned
Answer: d
10. Which of the following is not an OOPS concept in Java?
a) Polymorphism
b) Inheritance
c) Compilation
d) Encapsulation
Answer: c
11. What is not the use of “this” keyword in Java?
a) Referring to the instance variable when a local variable has the same name
b) Passing itself to the method of the same class
c) Passing itself to another method
d) Calling another constructor in constructor chaining
Answer: b
12. What will be the output of the following Java program?

    class variable_scope 
    {
        public static void main(String args[]) 
        {
            int x;
            x = 5;
            {
	        int y = 6;
	        System.out.print(x + " " + y);
            }
            System.out.println(x + " " + y);
        } 
    }

a) Compilation error
b) Runtime error
c) 5 6 5 6
d) 5 6 5
Answer: a
13. What will be the error in the following Java code?

    byte b = 50;
    b = b * 50;

a) b cannot contain value 50
b) b cannot contain value 100, limited by its range
c) No error in this code
d) * operator has converted b * 50 into int, which can not be converted to byte without casting
Answer: d
14. Which of the following is a type of polymorphism in Java Programming?
a) Multiple polymorphism
b) Compile time polymorphism
c) Multilevel polymorphism
d) Execution time polymorphism
Answer: b
15. What will be the output of the following Java program ?

    class leftshift_operator 
    {
        public static void main(String args[]) 
        {        
             byte x = 64;
             int i;
             byte y; 
             i = x << 2;
             y = (byte) (x << 2)
             System.out.print(i + " " + y);
        } 
    }

a) 0 256
b) 0 64
c) 256 0
d) 64 0
Answer: c
16. What will be the output of the following Java code?

    class box 
    {
        int width;
        int height;
        int length;
    } 
    class main
    {
        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) 100
b) 400
c) 200
d) 12
Answer: c
17. What is Truncation in Java?
a) Floating-point value assigned to a Floating type
b) Floating-point value assigned to an integer type
c) Integer value assigned to floating type
d) Integer value assigned to floating type
Answer: b
18. 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 3 4 5
b) 1 2 3 4
c) 1 2
d) 1 2 3
Answer: d
19. What will be the output of the following Java code snippet?

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

a) The snippet compiles and runs but does not print anything
b) The snippet compiles, runs and prints 0
c) The snippet compiles, runs and prints 1
d) The snippet does not compile
Answer: a
20. What is the extension of compiled java classes?
a) .txt
b) .js
c) .class
d) .java
Answer: c
21. Which exception is thrown when java is out of memory?
a) MemoryError
b) OutOfMemoryError
c) MemoryOutOfBoundsException
d) MemoryFullException
Answer: b
22. What will be the output of the following Java code?

    class String_demo 
    {
        public static void main(String args[])
        {
            char chars[] = {'a', 'b', 'c'};
            String s = new String(chars);
            System.out.println(s);
        }
   }

a) abc
b) a
c) b
d) c
Answer: a
23. Which of these are selection statements in Java?
a) break
b) continue
c) for()
d) if()
Answer: d
24. 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) 1
b) 120
c) 0
d) None of the mentioned
Answer: a
25. What will be the output of the following Java code?

    class output 
    {
        public static void main(String args[])
        { 
           String c = "Hello i love java";
           boolean var;
           var = c.startsWith("hello");
           System.out.println(var);
        }
    }

a) 0
b) true
c) 1
d) false
Answer: d
26. Which of these keywords is used to define interfaces in Java?
a) intf
b) Intf
c) interface
d) Interface
Answer: c
27. What will be the output of the following Java program?

    class output 
    {
        public static void main(String args[])
        { 
           StringBuffer s1 = new StringBuffer("Quiz");
           StringBuffer s2 = s1.reverse();
           System.out.println(s2);
        }
    }

a) QuizziuQ
b) ziuQQuiz
c) Quiz
d) ziuQ
Answer: d
28. What will be the output of the following Java code?

    class Output 
    {
        public static void main(String args[]) 
        {
            Integer i = new Integer(257);  
            byte x = i.byteValue();
            System.out.print(x);
        }
    }

a) 257
b) 256
c) 1
d) 0
Answer: c
29. What will be the output of the following Java program?

    class Output 
    {
         public static void main(String args[]) 
        {
            double x = 2.0;  
            double y = 3.0;
            double z = Math.pow( x, y );
            System.out.print(z);
        }
    }

a) 9.0
b) 8.0
c) 4.0
d) 2.0
Answer: b
30. Which of the following is a superclass of every class in Java?
a) ArrayList
b) Abstract class
c) Object class
d) String
Answer: c
31. What will be the output of the following Java code?

    class Output 
    {
         public static void main(String args[]) 
         {
             double x = 3.14;  
             int y = (int) Math.ceil(x);
             System.out.print(y);
         }
    }

a) 3
b) 0
c) 4
d) 3.0
Answer: c
32. What will be the output of the following Java program?

    import java.net.*;
    class networking 
    {
        public static void main(String[] args) throws Exception 
        {
            URL obj = new URL("https://www.progies.in/java");
            URLConnection obj1 = obj.openConnection();
            int len = obj1.getContentLength();
            System.out.print(len);
        }
    }

Note: Host URL is having length of content 127.
a) 127
b) 126
c) Runtime Error
d) Compilation Error
Answer: a
33. Which of the below is not a Java Profiler?
a) JProfiler
b) Eclipse Profiler
c) JVM
d) JConsole
Answer: c
34. What will be the output of the following Java program?

    import java.net.*;
    class networking
    {
        public static void main(String[] args) throws MalformedURLException
        {
            URL obj = new URL("https://www.progies.in/java");
            System.out.print(obj.toExternalForm());
        }
    }

a) www.progies.in
b) https://www.progies.in/java
c) progies
d) progies.in
Answer: b
35. What will be the output of the following Java code snippet?

    import java.util.*;
    class Arraylists 
    {
        public static void main(String args[]) 
        {
            ArrayLists obj = new ArrayLists();
            obj.add("A");
            obj.add("B");
            obj.add("C");
            obj.add(1, "D");
            System.out.println(obj);
        }
    }

a) [A, D, C]
b) [A, B, C]
c) [A, B, C, D]
d) [A, D, B, C]
Answer: d
36. Which of these packages contains the exception Stack Overflow in Java?
a) java.io
b) java.system
c) java.lang
d) java.util
Answer: c
37. What will be the output of the following Java program?

    import java.util.*;
    class Collection_iterators 
    {
        public static void main(String args[]) 
        {
            LinkedList list = new LinkedList();
            list.add(new Integer(2));
            list.add(new Integer(8));
            list.add(new Integer(5));
            list.add(new Integer(1));
            Iterator i = list.iterator();
            Collections.reverse(list);
	    Collections.sort(list);
            while(i.hasNext())
	        System.out.print(i.next() + " ");
        }
    }

a) 1 2 5 8
b) 2 1 8 5
c) 1 5 8 2
d) 2 8 5 1
Answer: a
38. Which of these statements is incorrect about Thread?
a) start() method is used to begin execution of the thread
b) run() method is used to begin execution of a thread before start() method in special cases
c) A thread can be formed by implementing Runnable interface only
d) A thread can be formed by a class that extends Thread class
Answer: b
39. Which of these keywords are used for the block to be examined for exceptions?
a) check
b) throw
c) catch
d) try
Answer: d
40. What will be the output of the following Java code?

    class newthread extends Thread
    {
	Thread t;
	newthread()
        {
	    t1 = new Thread(this,"Thread_1");
	    t2 = new Thread(this,"Thread_2");
	    t1.start();
	    t2.start();
	}
	public void run()
        {
	    t2.setPriority(Thread.MAX_PRIORITY);	
	    System.out.print(t1.equals(t2));
        }    
    }
    class multithreaded_programing
    {
        public static void main(String args[])
        {
            new newthread();        
        }
    }

a) truetrue
b) falsefalse
c) true
d) false
Answer: b
41. Which one of the following is not an access modifier?
a) Protected
b) Void
c) Public
d) Private
Answer: b
42. What will be the output of the following Java program?

   final class A 
    {
         int i;
    }    
    class B extends A 
    {
        int j;
        System.out.println(j + " " + i);  
    }    
    class inheritance 
    {
        public static void main(String args[])
        {
            B obj = new B();
            obj.display();     
        }
   }

a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
Answer: d
43. What is the numerical range of a char data type in Java?
a) 0 to 256
b) -128 to 127
c) 0 to 65535
d) 0 to 32767
Answer: c
44. Which class provides system independent server side implementation?
a) Server
b) ServerReader
c) Socket
d) ServerSocket
Answer: d
45. What will be the output of the following Java program?

   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) 4 6.4
b) 6.4 6
c) 6.4 6.4
d) 6 6
Answer: a
46. Which of the following is true about servlets?
a) Servlets can use the full functionality of the Java class libraries
b) Servlets execute within the address space of web server, platform independent and uses the functionality of java class libraries
c) Servlets execute within the address space of web server
d) Servlets are platform-independent because they are written in java
Answer: b