Java MCQ Questions and Answers on Fundamental

Java MCQ Questions and Answers on Fundamental Here provide java multiple choice questions with answers. you can learn and practice mcq questions.

Java MCQ Questions and Answers on Fundamental

1. Who invented Java Programming?

a) Guido van Rossum
b) James Gosling
c) Dennis Ritchie
d) Bjarne Stroustrup

Answer: b
Explanation: Java programming was developed by James Gosling at Sun Microsystems in 1995. James Gosling is well known as the father of Java.

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
Explanation: Java is called ‘Platform Independent Language’ as it primarily works on the principle of ‘compile once, run everywhere’.

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

a) JRE
b) JIT
c) JDK
d) JVM

Answer: c
Explanation: JDK is a core component of Java Environment and provides all the tools, executables and binaries required to compile, debug and execute a Java Program.

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
Explanation: Pointers is not a Java feature. Java provides an efficient abstraction layer for developing without using a pointer in Java. Features of Java Programming are Portable, Architectural Neutral, Object-Oriented, Robust, Secure, Dynamic and Extensible, etc.

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
Explanation: Keywords are specially reserved words that can not be used for naming a user-defined variable, for example: class, int, for, etc.

6. What is the extension of java code files?

a) .js
b) .txt
c) .class
d) .java

Answer: d
Explanation: Java files have .java extension.

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
Explanation: Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32.
output:

$ javac increment.java 
$ java increment 
32

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

a) MAVEN_Path
b) JavaPATH
c) JAVA
d) JAVA_HOME

Answer: d
Explanation: JAVA_HOME is used to store a path to the java installation.

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

a) Polymorphism
b) Inheritance
c) Compilation
d) Encapsulation

Answer: c
Explanation: There are 4 OOPS concepts in Java. Inheritance, Encapsulation, Polymorphism and Abstraction.

10. 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
Explanation: “this” is an important keyword in java. It helps to distinguish between local variable and variables passed in the method as parameters.

12. 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
Explanation: While evaluating an expression containing int, bytes or shorts, the whole expression is converted to int then evaluated and the result is also of type int.

13. 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
Explanation: There are two types of polymorphism in Java. Compile time polymorphism (overloading) and runtime polymorphism (overriding).

14. 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
Explanation: None.
output:

$ javac leftshift_operator.java
$ java leftshift_operator
256 0

15. 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
Explanation: None.
output:

$ javac main.java
$ java main
200

16. 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
Explanation: None.

17. 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
Explanation: arr.length() is 5, so the loop is executed for three times.
output:

$ javac Output.java
$ java Output
1 2 3

18. 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
Explanation: As no argument is passed to the code, the length of args is 0. So the code will not print.

19. What is the extension of compiled java classes?

a) .txt
b) .js
c) .class
d) .java

Answer: c
Explanation: The compiled java files have .class extension.

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

a) MemoryError
b) OutOfMemoryError
c) MemoryOutOfBoundsException
d) MemoryFullException

Answer: b
Explanation: The Xms flag has no default value, and Xmx typically has a default value of 256MB. A common use for these flags is when you encounter a java.lang.OutOfMemoryError.

21. Which of these are selection statements in Java?

a) break
b) continue
c) for()
d) if()

Answer: d
Explanation: Continue and break are jump statements, and for is a looping statement.

Be the first to comment

Leave a Reply

Your email address will not be published.


*