JAVA Basics MCQs

  1. What will be printed?
    1 System.out.println(2 + 3 * 4);
    a) 20
    b) 14
    c) 24
    d) 12
    View Answer

    Correct answer: B — 14

  2. Which of the following is not a primitive type in Java?
    a) int
    b) float
    c) String
    d) boolean
    View Answer

    Correct answer: C — String

  3. What will be printed?
    1 System.out.println("Hello" + "World");
    a) Hello World
    b) HelloWorld
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — HelloWorld

  4. Which keyword is used to define a constant in Java?
    a) const
    b) final
    c) static
    d) constant
    View Answer

    Correct answer: B — final

  5. What will be printed?
    1 int x = 5;
    2 System.out.println(x++);
    a) 5
    b) 6
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — 5

  6. Which of the following is the default value of a boolean in Java?
    a) true
    b) false
    c) null
    d) 0
    View Answer

    Correct answer: B — false

  7. What will be printed?
    1 int a = 10;
    2 int b = 20;
    3 System.out.println(a > b);
    a) true
    b) false
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — false

  8. Which of the following is used to terminate a statement in Java?
    a) :
    b) ;
    c) ,
    d) .
    View Answer

    Correct answer: B — ;

  9. What will be printed?
    1 System.out.println(10/3);
    a) 3
    b) 3.33
    c) 3.0
    d) Compilation error
    View Answer

    Correct answer: A — 3

  10. Which of the following is the entry point of a Java program?
    a) public void start()
    b) public static void main(String[] args)
    c) public void run()
    d) public static void init()
    View Answer

    Correct answer: B — public static void main(String[] args)

  11. What will be printed?
    1 System.out.println(5 == 5);
    a) true
    b) false
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — true

  12. Which of the following is used to allocate memory dynamically in Java?
    a) malloc
    b) new
    c) alloc
    d) create
    View Answer

    Correct answer: B — new

  13. What will be printed?
    1 System.out.println("Java".length());
    a) 3
    b) 4
    c) 5
    d) Compilation error
    View Answer

    Correct answer: B — 4

  14. Which of the following is not a valid access modifier in Java?
    a) public
    b) private
    c) protected
    d) internal
    View Answer

    Correct answer: D — internal

  15. What will be printed?
    1 System.out.println(10 % 3);
    a) 1
    b) 2
    c) 3
    d) 0
    View Answer

    Correct answer: A — 1

  16. Which of the following is used to compare two strings in Java?
    a) ==
    b) equals()
    c) compare()
    d) compareTo()
    View Answer

    Correct answer: B — equals()

  17. What will be printed?
    1 System.out.println("A" + 1);
    a) A1
    b) 65
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — A1

  18. Which of the following is the default value of an int in Java?
    a) 0
    b) 1
    c) null
    d) undefined
    View Answer

    Correct answer: A — 0

  19. What will be printed?
    1 System.out.println(true && false);
    a) true
    b) false
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — false

  20. Which of the following is used to handle exceptions in Java?
    a) try-catch
    b) if-else
    c) switch
    d) loop
    View Answer

    Correct answer: A — try-catch

  21. What will be printed?
    1 System.out.println(2 << 2);
    a) 4
    b) 8
    c) 2
    d) 16
    View Answer

    Correct answer: B — 8

  22. Which of the following is used to stop a loop in Java?
    a) break
    b) continue
    c) exit
    d) stop
    View Answer

    Correct answer: A — break

  23. What will be printed?
    1 System.out.println("abc".toUpperCase());
    a) abc
    b) ABC
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — ABC

  24. Which of the following is used to define a package in Java?
    a) package
    b) import
    c) namespace
    d) module
    View Answer

    Correct answer: A — package

  25. What will be printed?
    1 System.out.println(5/2.0);
    a) 2
    b) 2.5
    c) 2.0
    d) Compilation error
    View Answer

    Correct answer: B — 2.5

  26. Which of the following is used to create a thread in Java?
    a) Thread class
    b) Runnable interface
    c) Both A and B
    d) None
    View Answer

    Correct answer: C — Both A and B

  27. What will be printed?
    1 System.out.println("Java".charAt(2));
    a) J
    b) a
    c) v
    d) Compilation error
    View Answer

    Correct answer: C — v

  28. Which of the following is used to import classes in Java?
    a) package
    b) import
    c) include
    d) namespace
    View Answer

    Correct answer: B — import

  29. What will be printed?
    1 System.out.println(10 == 10.0);
    a) true
    b) false
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — true

  30. Which of the following is used to define an interface in Java?
    a) interface
    b) abstract
    c) class
    d) implements
    View Answer

    Correct answer: A — interface

  31. What will be printed?
    1 System.out.println("Hello".concat("World"));
    a) Hello World
    b) HelloWorld
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — HelloWorld

  32. Which of the following is used to exit from a program in Java?
    a) break
    b) System.exit()
    c) stop
    d) return
    View Answer

    Correct answer: B — System.exit()

  33. What will be printed?
    1 System.out.println(3 > 2 ? "Yes" : "No");
    a) Yes
    b) No
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — Yes

  34. Which of the following is used to define a class in Java?
    a) class
    b) object
    c) new
    d) define
    View Answer

    Correct answer: A — class

  35. What will be printed?
    1 System.out.println("Java".equals("JAVA"));
    a) true
    b) false
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: B — false

  36. Which of the following is used to define a constructor in Java?
    a) Same name as class
    b) init()
    c) new()
    d) constructor()
    View Answer

    Correct answer: A — Same name as class

  37. What will be printed?
    1 System.out.println("abc".replace("a","z"));
    a) zbc
    b) abc
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — zbc

  38. Which of the following is used to define a constant reference in Java?
    a) final
    b) const
    c) static
    d) constant
    View Answer

    Correct answer: A — final

  39. What will be printed?
    1 System.out.println(7/2);
    a) 3
    b) 3.5
    c) 4
    d) Compilation error
    View Answer

    Correct answer: A — 3

  40. Which of the following is used to create an object in Java?
    a) new
    b) malloc
    c) alloc
    d) create
    View Answer

    Correct answer: A — new

  41. What will be printed?
    1 System.out.println("Java".toLowerCase());
    a) java
    b) JAVA
    c) Java
    d) Compilation error
    View Answer

    Correct answer: A — java

  42. Which of the following is used to define inheritance in Java?
    a) extends
    b) implements
    c) inherits
    d) super
    View Answer

    Correct answer: A — extends

  43. What will be printed?
    1 System.out.println(2 == 2.0);
    a) true
    b) false
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — true

  44. Which of the following is used to define polymorphism in Java?
    a) Overloading and Overriding
    b) Inheritance only
    c) Encapsulation only
    d) Abstraction only
    View Answer

    Correct answer: A — Overloading and Overriding

  45. What will be printed?
    1 System.out.println("Hello".indexOf("l"));
    a) 1
    b) 2
    c) 3
    d) Compilation error
    View Answer

    Correct answer: B — 2

  46. Which of the following is used to define abstraction in Java?
    a) abstract class
    b) interface
    c) Both A and B
    d) None
    View Answer

    Correct answer: C — Both A and B

  47. What will be printed?
    1 System.out.println("Java".substring(1,3));
    a) Ja
    b) av
    c) va
    d) Compilation error
    View Answer

    Correct answer: B — av

  48. Which of the following is used to define encapsulation in Java?
    a) private variables with public getters/setters
    b) inheritance
    c) polymorphism
    d) abstraction
    View Answer

    Correct answer: A — private variables with public getters/setters

  49. What will be printed?
    1 System.out.println("abc".equalsIgnoreCase("ABC"));
    a) true
    b) false
    c) Compilation error
    d) Runtime exception
    View Answer

    Correct answer: A — true

  50. Which of the following is used to define multiple inheritance in Java?
    a) class inheritance
    b) interface implementation
    c) abstract class
    d) super keyword
    View Answer

    Correct answer: B — interface implementation

Quick Links to Explore