Home » Java MCQ Answers

Java MCQ Answers

Java MCQ Answers. Get all of the validated answers for the java mcq. Ideal for quick and fast review or getting ready for the actual test. if you have short time then you are on right place.

Java MCQ Answers

This is the java mcq with 100% correct answers:

Q1. Which one of the following is not an access modifier?

  1. protected
  2. public
  3. private

Q2. What will be the output of the following Java program?

1.	   final class A 
2.	    {
3.	         int i;
4.	    }    
5.	    class B extends A 
6.	    {
7.	        int j;
8.	        System.out.println(j + " " + i);  
9.	    }    
10.	    class inheritance 
11.	    {
12.	        public static void main(String args[])
13.	        {
14.	            B obj = new B();
15.	            obj.display();     
16.	        }
17.	   }
  1. 2 2
  2. 3 3
  3. Runtime Error

Q3. What is the numerical range of a char data type in Java?

  1. 0 to 256
  2. -128 to 127
  3. 0 to 32767

Q4. Which class provides system independent server side implementation?

  1. Server
  2. ServerReader
  3. Socket

Q5. What will be the output of the following Java program?

1.	   class overload 
2.	   {
3.	        int x;
4.	 	double y;
5.	        void add(int a , int b) 
6.	        {
7.	            x = a + b;
8.	        }
9.	        void add(double c , double d)
10.	        {
11.	            y = c + d;
12.	        }
13.	        overload() 
14.	        {
15.	            this.x = 0;
16.	            this.y = 0;
17.	        }        
18.	    }    
19.	    class Overload_methods 
20.	    {
21.	        public static void main(String args[])
22.	        {
23.	            overload obj = new overload();   
24.	            int a = 2;
25.	            double b = 3.2;
26.	            obj.add(a, a);
27.	            obj.add(b, b);
28.	            System.out.println(obj.x + " " + obj.y);     
29.	        }
30.	   }
  1. 6.4 6
  2. 6.4 6.4
  3. 6 6

Q6. Which of the following is true about servlets?

  1. Servlets can use the full functionality of the Java class libraries
  2. Servlets execute within the address space of web server
  3. Servlets are platform-independent because they are written in java

Q7. What will be the output of the following Java code?

1.	    class increment {
2.	        public static void main(String args[]) 
3.	        {        
4.	             int g = 3;
5.	             System.out.print(++g * 8);
6.	        } 
7.	    }
  1. 33
  2. 24
  3. 25

Q8. Which environment variable is used to set the java path?

  1. MAVEN_Path
  2. JavaPATH
  3. JAVA

Q9. What will be the output of the following Java program?

1.	class output {
2.	        public static void main(String args[]) 
3.	        {
4.	            double a, b,c;
5.	            a = 3.0/0;
6.	            b = 0/4.0;
7.	            c=0/0.0;
8.	 
9.		    System.out.println(a);
10.	            System.out.println(b);
11.	            System.out.println(c);
12.	        } 
13.	    }
  1. NaN
  2. Infinity
  3. 0.0

Q10. Which of the following is not an OOPS concept in Java?

  1. Polymorphism
  2. Inheritance
  3. Encapsulation

Q11. What is not the use of “this” keyword in Java?

  1. Referring to the instance variable when a local variable has the same name
  2. Passing itself to another method
  3. Calling another constructor in constructor chaining

Q12. What will be the output of the following Java program?

1.	    class variable_scope 
2.	    {
3.	        public static void main(String args[]) 
4.	        {
5.	            int x;
6.	            x = 5;
7.	            {
8.		        int y = 6;
9.		        System.out.print(x + " " + y);
10.	            }
11.	            System.out.println(x + " " + y);
12.	        } 
13.	    }
  1. Runtime error
  2. 5 6 5 6
  3. 5 6 5

Q13. What will be the error in the following Java code?

  byte b = 50;
    b = b * 50;
  1. b cannot contain value 50
  2. b cannot contain value 100, limited by its range
  3. No error in this code

Q14. Which of the following is a type of polymorphism in Java Programming?

  1. Multiple polymorphism
  2. Multilevel polymorphism
  3. Execution time polymorphism

Q15. What will be the output of the following Java program?

1.	    class leftshift_operator 
2.	    {
3.	        public static void main(String args[]) 
4.	        {        
5.	             byte x = 64;
6.	             int i;
7.	             byte y; 
8.	             i = x << 2;
9.	             y = (byte) (x << 2);
10.	             System.out.print(i + " " + y);
11.	        } 
12.	    }
  1. 0 256
  2. 0 64
  3. 64 0

Q16. What will be the output of the following Java code?

1.	    class box 
2.	    {
3.	        int width;
4.	        int height;
5.	        int length;
6.	    } 
7.	    class main
8.	    {
9.	        public static void main(String args[]) 
10.	        {        
11.	             box obj = new box();
12.	             obj.width = 10;
13.	             obj.height = 2;
14.	             obj.length = 10;
15.	             int y = obj.width * obj.height * obj.length; 
16.	             System.out.print(y);
17.	        } 
18.	    }
  1. 100
  2. 400
  3. 12

Q17. What is Truncation in Java?

  1. Floating-point value assigned to a Floating type
  2. Integer value assigned to floating type
  3. Integer value assigned to floating type

Q18. What will be the output of the following Java program?

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

Q19. What will be the output of the following Java code snippet?

1.	class abc
2.	{
3.	    public static void main(String args[])
4.	    {
5.	        if(args.length>0)
6.	        System.out.println(args.length);
7.	    }
8.	}
  1. The snippet compiles, runs and prints 0
  2. The snippet compiles, runs and prints 1
  3. The snippet does not compile

Q20. What is the extension of compiled java classes?

  1. .txt
  2. .js
  3. .java

Q21. Which exception is thrown when java is out of memory?

  1. MemoryError
  2. MemoryOutOfBoundsException
  3. MemoryFullException

Q22. What will be the output of the following Java code?

1.	    class String_demo 
2.	    {
3.	        public static void main(String args[])
4.	        {
5.	            char chars[] = {'a', 'b', 'c'};
6.	            String s = new String(chars);
7.	            System.out.println(s);
8.	        }
9.	   }
  1. a
  2. b
  3. c

Q23. Which of these are selection statements in Java?

  1. break
  2. continue
  3. for()

Q24. What will be the output of the following Java program?

1.	    class recursion 
2.	    {
3.	        int func (int n) 
4.	        {
5.	            int result;
6.	            if (n == 1)
7.	                return 1;
8.	            result = func (n - 1);
9.	            return result;
10.	        }
11.	    } 
12.	    class Output 
13.	    {
14.	        public static void main(String args[]) 
15.	        {
16.	            recursion obj = new recursion() ;
17.	            System.out.print(obj.func(5));
18.	        }
19.	    }
  1. 120
  2. 0
  3. None of the mentioned

Q25. What will be the output of the following Java code?

1.	    class output 
2.	    {
3.	        public static void main(String args[])
4.	        { 
5.	           String c = "Hello i love java";
6.	           boolean var;
7.	           var = c.startsWith("hello");
8.	           System.out.println(var);
9.	        }
10.	    }
  1. 0
  2. true
  3. 1

Q26. Which of these keywords is used to define interfaces in Java?

  1. intf
  2. Intf
  3. Interface

Q27. What will be the output of the following Java program?

1.	    class output 
2.	    {
3.	        public static void main(String args[])
4.	        { 
5.	           StringBuffer s1 = new StringBuffer("Quiz");
6.	           StringBuffer s2 = s1.reverse();
7.	           System.out.println(s2);
8.	        }
9.	    }
  1. QuizziuQ
  2. ziuQQuiz
  3. Quiz

Q28. What will be the output of the following Java code?

1.	    class Output 
2.	    {
3.	        public static void main(String args[]) 
4.	        {
5.	            Integer i = new Integer(257);  
6.	            byte x = i.byteValue();
7.	            System.out.print(x);
8.	        }
9.	    }
  1. 257
  2. 256
  3. 0

Q29. What will be the output of the following Java program?

1.	    class Output 
2.	    {
3.	         public static void main(String args[]) 
4.	        {
5.	            double x = 2.0;  
6.	            double y = 3.0;
7.	            double z = Math.pow( x, y );
8.	            System.out.print(z);
9.	        }
10.	    }
  1. 9.0
  2. 4.0
  3. 2.0

Q30. Which of the following is a superclass of every class in Java?

  1. ArrayList
  2. Abstract class
  3. String

Q31. 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);
         }
    }
  1. 3
  2. 0
  3. 3.0

Q32. What will be the output of the following Java program?

1.	    import java.net.*;
2.	    class networking 
3.	    {
4.	        public static void main(String[] args) throws Exception 
5.	        {
6.	            URL obj = new URL("https://www.sanfoundry.com/javamcq");
7.	            URLConnection obj1 = obj.openConnection();
8.	            int len = obj1.getContentLength();
9.	            System.out.print(len);
10.	        }
11.	    }
  1. 126
  2. Runtime Error
  3. Compilation Error

Q33. Which of the below is not a Java Profiler?

  1. JProfiler
  2. Eclipse Profiler
  3. JConsole

Q34. What will be the output of the following Java program?

1.	    import java.net.*;
2.	    class networking
3.	    {
4.	        public static void main(String[] args) throws MalformedURLException
5.	        {
6.	            URL obj = new URL("https://www.getexamanswers.com/java-mcq-answers");
7.	            System.out.print(obj.toExternalForm());
8.	        }
9.	    }
  1. www.getexamanswers.com
  2. getexamanswers
  3. getexamanswers.com

Q35. What will be the output of the following Java code snippet?

1.	   import java.util.*;
2.	   class Arraylists
3.	   {
4.	       public static void main(String args[])
5.	       {
6.	           ArrayList obj = new ArrayList();
7.	           obj.add("A");
8.	           obj.add("B");
9.	           obj.add("C");
10.	           obj.add(1, "D");
11.	           System.out.println(obj);
12.	       }
13.	   }
  1. [A, D, C]
  2. [A, B, C]
  3. [A, B, C, D]

36. Which of these packages contains the error StackOverflowError in Java?

  1. java.io
  2. java.system
  3. java.util

Q37. What will be the output of the following Java program?

1.	    import java.util.*;
2.	    class Collection_iterators 
3.	    {
4.	        public static void main(String args[]) 
5.	        {
6.	            LinkedList list = new LinkedList();
7.	            list.add(new Integer(2));
8.	            list.add(new Integer(8));
9.	            list.add(new Integer(5));
10.	            list.add(new Integer(1));
11.	            Iterator i = list.iterator();
12.	            Collections.reverse(list);
13.		    Collections.sort(list);
14.	            while(i.hasNext())
15.		        System.out.print(i.next() + " ");
16.	        }
17.	    }
  1. 2 1 8 5
  2. 1 5 8 2
  3. 2 8 5 1

Q38. Which of these statements is incorrect about Thread?

  1. start() method is used to begin execution of the thread
  2. A thread can be formed by implementing Runnable interface only
  3. A thread can be formed by a class that extends Thread class

Q39. Which of these keywords are used for the block to be examined for exceptions?

  1. check
  2. throw
  3. catch

Q40. What will be the output of the following Java code?

1.	   class newthread extends Thread
2.	   {
3.		Thread t1, t2;
4.		newthread()
5.	       {
6.		    t1 = new Thread(this,"Thread_1");
7.		    t2 = new Thread(this,"Thread_2");
8.		    t1.start();
9.		    t2.start();
10.		}
11.		public void run()
12.	       {
13.		    t2.setPriority(Thread.MAX_PRIORITY);	
14.		    System.out.print(t1.equals(t2));
15.	       }   
16.	   }
17.	   class multithreaded_programing
18.	   {
19.	       public static void main(String args[])
20.	       {
21.	           new newthread();       
22.	       }
23.	   }
  1. truetrue
  2. true
  3. false

Q41. Who invented Java Programming?

  1. Guido van Rossum
  2. Dennis Ritchie
  3. Bjarne Stroustrup

Q42. Which statement is true about Java?

  1. Java is a sequence-dependent programming language
  2. Java is a code dependent programming language
  3. Java is a platform-dependent programming language

Q43. Which component is used to compile, debug and execute the java programs?

  1. JRE
  2. JIT
  3. JVM

Q44. Which one of the following is not a Java feature?

  1. Object-oriented
  2. Portable
  3. Dynamic and Extensible

Q45. Which of these cannot be used for a variable name in Java?

  1. identifier & keyword
  2. identifier
  3. none of the mentioned

Q46. What is the extension of java code files?

  1. .js
  2. .txt
  3. .class

Also Read: Python MCQ Answers

Note: If you have the new question on this test, please comment Question and Multiple-Choice list in form below this article. We will update answers for you in the shortest time. Thank you! We truly value your contribution to the website.

Leave a Reply

Your email address will not be published. Required fields are marked *