Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views21 pages

Java Basics - 50 MCQs With Answers

Uploaded by

saiakshaykv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views21 pages

Java Basics - 50 MCQs With Answers

Uploaded by

saiakshaykv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Java Basics – 50 MCQs with Answers

1. What is the entry point of a Java program?

A) class main()​
B) public void main(String[] args)​
C) public static void main(String[] args)​
D) main() method inside any class

Answer – C) public static void main(String[] args)

2. Why is the main() method static in Java?

A) To allow JVM to call it without creating an object​


B) Because all methods in Java must be static​
C) To save memory​
D) To allow multiple classes to run simultaneously

Answer – A) To allow JVM to call it without creating an


object

3. Which of the following correctly represents


the order?
A) JVM > JRE > JDK​
B) JDK > JRE > JVM​
C) JRE > JVM > JDK​
D) JDK > JVM > JRE

Answer – B) JDK > JRE > JVM

4. Which component converts Java code into


bytecode?

A) JVM​
B) JRE​
C) javac compiler​
D) JDK

Answer – C) javac compiler

5. Which of the following is NOT true about


variables?

A) Local variables don’t have default values​


B) Instance variables are unique to each object​
C) Static variables are shared by all objects​
D) Local variables are stored in the heap

Answer – D) Local variables are stored in the heap


6. Can a local variable have a default value in
Java?

A) Yes​
B) No​
C) Only for int​
D) Only for boolean

Answer – B) No

7. Which of the following is a primitive data


type?

A) String​
B) Array​
C) int​
D) Object

Answer – C) int

8. Why is char 2 bytes in Java?

A) To support ASCII​
B) To support Unicode characters​
C) To support binary values​
D) To save memory
Answer – B) To support Unicode characters

9. What is the default value of a boolean


variable in Java?

A) true​
B) false​
C) 0​
D) null

Answer – B) false

10. Which operator is used for comparing


object references in Java?

A) .equals()​
B) ==​
C) compare()​
D) compareTo()

Answer – B) ==

11. Which operator is used to compare


contents of objects (like Strings)?
A) ==​
B) equals()​
C) compareTo()​
D) equalsIgnoreCase()

Answer – B) equals()

12. What will be the output of this code?


String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2);

A) true​
B) false​
C) null​
D) Error

Answer – B) false

13. What will be the output of this code?


String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2);
A) true​
B) false​
C) null​
D) Error

Answer – A) true

14. Which loop guarantees at least one


execution?

A) for loop​
B) while loop​
C) do-while loop​
D) enhanced for loop

Answer – C) do-while loop

15. Which keyword is used to define a block of


multiple options in Java?

A) if​
B) else-if​
C) switch​
D) case

Answer – C) switch
16. Which operator is called the ternary
operator in Java?

A) &&​
B) ?:​
C) ++​
D) ==

Answer – B) ?:

17. Which of these is a relational operator?

A) +​
B) ==​
C) &&​
D) =

Answer – B) ==

18. What is the size of an int in Java?

A) 2 bytes​
B) 4 bytes​
C) 8 bytes​
D) Depends on OS

Answer – B) 4 bytes
19. Which of these control structures is best
suited when the number of iterations is
known?

A) while​
B) do-while​
C) for​
D) if-else

Answer – C) for

20. What will be the output?


int a = 10;
int b = 20;
System.out.println(a > b ? a : b);

A) 10​
B) 20​
C) true​
D) false

Answer – B) 20
21. Which of these is not a valid identifier in
Java?

A) myVar​
B) _count​
C) 123abc​
D) $price

Answer – C) 123abc

22. Which keyword is used to define


constants in Java?

A) const​
B) final​
C) static​
D) constant

Answer – B) final

23. Which of these is not a primitive type in


Java?

A) long​
B) boolean​
C) String​
D) char

Answer – C) String

24. Which access modifier makes a variable


accessible only within the same class?

A) public​
B) protected​
C) private​
D) default

Answer – C) private

25. What is the default value of a reference


variable in Java?

A) 0​
B) null​
C) undefined​
D) empty string

Answer – B) null
26. Which of the following statements about
JVM is correct?

A) JVM is platform-independent​
B) JVM is platform-dependent​
C) JVM compiles source code​
D) JVM is part of JDK only

Answer – B) JVM is platform-dependent

27. Which of these can be overloaded in


Java?

A) Constructors only​
B) Methods only​
C) Both methods and constructors​
D) main() method only

Answer – C) Both methods and constructors

28. Which of the following cannot be used as


a variable name in Java?

A) class​
B) var​
C) my_var​
D) _name

Answer – A) class

29. Which operator is used for short-circuit


AND in Java?

A) &​
B) &&​
C) |​
D) ||

Answer – B) &&

30. What will be the output?

int x = 5;

System.out.println(x++ + ++x);

A) 10​
B) 11​
C) 12​
D) 13
Answer – D) 13

31. What is the size of double in Java?

A) 4 bytes​
B) 8 bytes​
C) 16 bytes​
D) Depends on OS

Answer – B) 8 bytes

32. Which keyword is used to exit a loop


immediately?

A) return​
B) continue​
C) break​
D) exit

Answer – C) break

33. Which keyword skips the current iteration


and continues with the next?

A) exit​
B) skip​
C) continue​
D) break

Answer – C) continue

34. Which of these cannot be declared as


static?

A) Variable​
B) Method​
C) Constructor​
D) Block

Answer – C) Constructor

35. Which type of memory is used to store


instance variables?

A) Stack​
B) Heap​
C) Method Area​
D) Register

Answer – B) Heap

36. Where are local variables stored in Java?


A) Heap​
B) Method Area​
C) Stack​
D) Permanent Generation

Answer – C) Stack

37. What is the default value of an


uninitialized int instance variable?

A) null​
B) 0​
C) garbage value​
D) undefined

Answer – B) 0

38. What will be the output?

int a = 10;

int b = 20;

int c = (a > b) ? a : b;

System.out.println(c);
A) 10​
B) 20​
C) 30​
D) Error

Answer – B) 20

39. Which of these is NOT a valid loop in Java?

A) for​
B) foreach​
C) while​
D) do-while

Answer – B) foreach (Java uses enhanced for loop, not


"foreach" keyword)

40. Which operator is used for object creation


in Java?

A) malloc​
B) new​
C) create​
D) alloc

Answer – B) new
41. Which symbol is used for single-line
comments in Java?

A) //​
B) /*​
C) <!--​
D) #

Answer – A) //

42. Which symbol is used for multi-line


comments in Java?

A) //​
B) /* … */​
C) <!-- … -->​
D) ## … ##

Answer – B) / … /

43. Which method is used to print output in


Java?

A) print()​
B) println()​
C) System.print()​
D) display()

Answer – B) println()

44. What will be the output?

System.out.println(10 + 20 + "Java");

A) Java30​
B) 30Java​
C) 1020Java​
D) Compilation error

Answer – B) 30Java

45. What will be the output?

System.out.println("Java" + 10 + 20);

A) Java30​
B) 30Java​
C) Java1020​
D) Compilation error
Answer – C) Java1020

46. Which keyword is used to create a


subclass in Java?

A) super​
B) extends​
C) implements​
D) inherits

Answer – B) extends

47. Which of these is NOT a valid data type


modifier in Java?

A) signed​
B) final​
C) abstract​
D) transient

Answer – A) signed

48. Which statement about Java is true?

A) Java supports multiple inheritance with classes​


B) Java supports multiple inheritance with interfaces​
C) Java does not support inheritance at all​
D) Java allows operator overloading

Answer – B) Java supports multiple inheritance with


interfaces

49. Which keyword is used to inherit from an


interface in Java?

A) extends​
B) implements​
C) inherit​
D) super

Answer – B) implements

50. Which method is called when an object is


created in Java?

A) init()​
B) constructor​
C) start()​
D) new()

Answer – B) constructor

You might also like