0% found this document useful (0 votes)
29 views5 pages

100 Java Programming MCQs with Answers

The document contains 100 multiple choice questions (MCQs) related to Java programming, covering topics such as Java basics, data types, operators, control structures, object-oriented programming, exception handling, arrays, file I/O, multithreading, GUI, and advanced concepts. Each question is accompanied by four answer options and the correct answer is provided. The document serves as a comprehensive review tool for students studying Java programming.

Uploaded by

munalroyal2
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)
29 views5 pages

100 Java Programming MCQs with Answers

The document contains 100 multiple choice questions (MCQs) related to Java programming, covering topics such as Java basics, data types, operators, control structures, object-oriented programming, exception handling, arrays, file I/O, multithreading, GUI, and advanced concepts. Each question is accompanied by four answer options and the correct answer is provided. The document serves as a comprehensive review tool for students studying Java programming.

Uploaded by

munalroyal2
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

100 Multiple Choice Questions (MCQs) with Answers: COS202 - Java Programming

Java Basics

1–10 [Already listed above]

Data Types and Variables

11–20 [Already listed above]

Operators and Expressions

21–30 [Already listed above]

Control Structures and Loops

31–40 [Already listed above]

Object-Oriented Programming

41. What is the default access modifier in Java?\ a) public\ b) private\ c) protected\ d) package-
private\ Answer: d

42. Which keyword is used to create a class object?\ a) class\ b) object\ c) new\ d) this\ Answer: c

43. Which keyword refers to the current object?\ a) this\ b) super\ c) current\ d) self\ Answer: a

44. What is method overloading?\ a) Defining multiple methods with same name but different
parameters\ b) Writing multiple classes\ c) Calling one method from another\ d) Using inheritance\
Answer: a

45. Which keyword is used to inherit a class?\ a) implement\ b) inherits\ c) super\ d) extends\
Answer: d

46. A class can extend:\ a) One class only\ b) Two classes\ c) Multiple classes\ d) No class\ Answer: a

47. Which of the following is true about constructors?\ a) Must have return type\ b) Must match class
name\ c) Must be static\ d) Can be abstract\ Answer: b

48. What is encapsulation?\ a) Hiding implementation details\ b) Inheriting methods\ c) Creating


subclasses\ d) Sharing code between objects\ Answer: a

1
49. Interface methods are by default:\ a) private\ b) public and abstract\ c) protected\ d) static\
Answer: b

50. Can a class implement multiple interfaces?\ a) No\ b) Only one\ c) Yes\ d) Only abstract classes\
Answer: c

Exception Handling

51. What keyword is used to handle exceptions?\ a) try\ b) catch\ c) throw\ d) All of the above\
Answer: d

52. Which class is the superclass of all exceptions?\ a) Object\ b) Throwable\ c) Runtime\ d) Exception\
Answer: b

53. Which block is always executed?\ a) try\ b) catch\ c) throw\ d) finally\ Answer: d

54. What is the use of ** throw **?\ a) To declare an exception\ b) To catch an exception\ c) To throw
an exception\ d) To ignore exceptions\ Answer: c

55. Which keyword is used to define a method may throw exception?\ a) catch\ b) throws\ c) try\ d)
handle\ Answer: b

Arrays and Strings

56. How do you declare an array in Java?\ a) int arr[];\ b) int[] arr;\ c) both a and b\ d) None of the
above\ Answer: c

57. What is the index of the first element in an array?\ a) -1\ b) 0\ c) 1\ d) Depends\ Answer: b

58. Which class is used to manipulate strings?\ a) Text\ b) String\ c) Array\ d) Character\ Answer: b

59. Strings in Java are:\ a) Mutable\ b) Immutable\ c) Static\ d) Changeable\ Answer: b

60. Which method compares two strings?\ a) equals()\ b) ==\ c) compareTo()\ d) Both a and c\ Answer:
d

File I/O and Packages

61. Which class is used to read a file?\ a) FileWriter\ b) FileReader\ c) Scanner\ d) All of the above\
Answer: d

62. What does the ** import ** keyword do?\ a) Create packages\ b) Load class libraries\ c) Read
input\ d) Export files\ Answer: b

63. Which package contains Scanner class?\ a) [Link]\ b) [Link]\ c) [Link]\ d) [Link]\ Answer: a

2
64. To write to a file in Java, you can use:\ a) PrintWriter\ b) FileWriter\ c) BufferedWriter\ d) All of the
above\ Answer: d

65. What does ** [Link]() ** return?\ a) Integer\ b) Boolean\ c) String\ d) File object\ Answer:
b

Multithreading and Concurrency

66. Which keyword is used to create a thread?\ a) thread\ b) Thread\ c) implements Runnable\ d) Both
b and c\ Answer: d

67. What is the method to start a thread?\ a) begin()\ b) start()\ c) init()\ d) run()\ Answer: b

68. Which interface must be implemented for multithreading?\ a) Threadable\ b) Runnable\ c)


Startable\ d) None\ Answer: b

69. Which method is used to delay execution?\ a) stop()\ b) sleep()\ c) wait()\ d) pause()\ Answer: b

70. Threads in Java belong to which package?\ a) [Link]\ b) [Link]\ c) [Link]\ d) [Link]\ Answer:
d

GUI and Java APIs

71. Which package is used for GUI in Java?\ a) [Link]\ b) [Link]\ c) Both a and b\ d) [Link]\
Answer: c

72. What is a JFrame?\ a) A type of class\ b) A top-level window\ c) A Java interface\ d) None\ Answer: b

73. Which component is used to take text input in Swing?\ a) JTextArea\ b) JLabel\ c) JTextField\ d)
JEditor\ Answer: c

74. Which method sets the size of a frame?\ a) setWidth()\ b) setSize()\ c) setLength()\ d) setBounds()\
Answer: b

75. Which layout arranges components in flow?\ a) BorderLayout\ b) GridLayout\ c) FlowLayout\ d)


CardLayout\ Answer: c

Advanced Concepts

76. What is polymorphism?\ a) Multiple constructors\ b) Overriding and overloading\ c) Inheritance\ d)


Typecasting\ Answer: b

77. Which of the following is not a primitive type?\ a) int\ b) float\ c) boolean\ d) String\ Answer: d

78. Which statement is true about Java?\ a) Java supports pointers\ b) Java is platform-independent\
c) Java does not support inheritance\ d) Java is purely procedural\ Answer: b

3
79. Which keyword prevents method overriding?\ a) private\ b) static\ c) final\ d) public\ Answer: c

80. Which of these can be declared abstract?\ a) Method\ b) Class\ c) Both\ d) Interface\ Answer: c

Mixed Review (81–100)

81. Which access modifier gives least access?\ a) public\ b) protected\ c) default\ d) private\ Answer:
d

82. The ** instanceof ** operator checks:\ a) Object type\ b) Class access\ c) Variable scope\ d)
Package type\ Answer: a

83. Which is used to store key-value pairs?\ a) ArrayList\ b) LinkedList\ c) HashMap\ d) HashSet\
Answer: c

84. Java was officially released in:\ a) 1991\ b) 1995\ c) 2000\ d) 1998\ Answer: b

85. Which keyword is used to stop inheritance?\ a) final\ b) static\ c) private\ d) sealed\ Answer: a

86. A ** null ** reference means:\ a) Zero value\ b) No object\ c) Blank string\ d) Empty array\
Answer: b

87. What will happen if main method is missing?\ a) Compilation error\ b) Runtime error\ c) Nothing\
d) Success\ Answer: b

88. Which class is the parent of all classes in Java?\ a) Parent\ b) Object\ c) Base\ d) Super\ Answer: b

89. ** static ** methods belong to:\ a) Instance\ b) Object\ c) Class\ d) Constructor\ Answer: c

90. Java supports which type of inheritance?\ a) Multiple via interfaces\ b) Multiple via classes\ c)
None\ d) Hybrid only\ Answer: a

91. What is the wrapper class for int?\ a) Int\ b) Integer\ c) Number\ d) Float\ Answer: b

92. Which keyword is used to define a class constant?\ a) const\ b) static final\ c) define\ d) constant\
Answer: b

93. Which class is used to create GUI dialogs?\ a) JOptionPane\ b) JFrame\ c) DialogBox\ d) GUIBox\
Answer: a

94. Java is known as:\ a) Object-oriented\ b) Platform-dependent\ c) Fast compiled language\ d) OS-
specific\ Answer: a

95. Which of these can hold duplicate elements?\ a) HashMap\ b) HashSet\ c) List\ d) TreeSet\
Answer: c

96. Which Java version introduced Lambda expressions?\ a) Java 6\ b) Java 7\ c) Java 8\ d) Java 9\
Answer: c

4
97. What is the default package in Java?\ a) [Link]\ b) [Link]\ c) [Link]\ d) [Link]\ Answer: c

98. Which stream handles object serialization?\ a) DataOutputStream\ b) FileWriter\ c)


ObjectOutputStream\ d) PrintWriter\ Answer: c

99. Which is not a feature of Java?\ a) Robust\ b) Portable\ c) Low-level\ d) Secure\ Answer: c

100. Java bytecode runs on:\ a) CPU directly\ b) Java compiler\ c) Java Virtual Machine\ d) Interpreter
only\ Answer: c

End of 100 MCQs for COS202 Java Programming. Let me know if you’d like this exported to PDF, Word,
or quizPDF foo

Common questions

Powered by AI

Using the "final" keyword in Java can denote two main implications. Firstly, when used with methods, it prevents method overriding. This means that if a method is declared as final, no subclass can override it, ensuring that the method's behavior remains constant in the hierarchy . Secondly, when used with classes, it prevents inheritance. A final class in Java cannot be extended by any other class, which ensures that the class's implementation cannot be altered by subclasses .

The "finally" block in Java exception handling is crucial for guaranteed execution of cleanup code regardless of whether an exception is thrown or not. It ensures that resources such as file streams or database connections are properly closed and released after a method execution completes. By providing a place for code that must execute following try-catch logic, the "finally" block enhances reliability and maintainability of code, shielding resource management from accidental bypass due to exceptions .

Lambda expressions, added in Java 8, streamline implementation of functional interfaces by offering a clear and concise way to represent single-method interfaces, introducing elements of functional programming into the language. They improve readability and brevity of code by eliminating the need for anonymous class syntax. This makes it easier to pass behavior as parameters, creating more abstract, flexible, and functional code structures, especially when combined with Java's powerful Stream API to handle collections and complex computations efficiently .

HashMap and HashSet both use hashing for storing elements, but they are designed for different use cases. HashMap stores key-value pairs, allowing retrieval of values based on associated keys, providing constant time complexity O(1) for insertion and lookup under ideal conditions. Conversely, HashSet uses a HashMap behind the scenes to store unique elements without key-value pair associations, ensuring no duplicates. HashMap's dual storage (key-value) occupies more memory than HashSet's single-element focus. HashMap is suitable for caching and fast lookups by keys, while HashSet is optimal for membership tests and operations needing unique elements .

Java's default access modifier, also known as package-private, provides a moderate level of encapsulation. Elements (fields, methods, or classes) with this access level are accessible only within the same package. This allows for a finer control over the encapsulation of classes and their members by restricting access from outside the package, thereby encouraging encapsulation at the package level. However, it is less restrictive than private access, which could lead to unintended interactions if all classes meant to be encapsulated are not entirely secure within the package boundaries .

In Java, interface methods are implicitly public and abstract, meaning they are meant to be implemented by classes rather than directly executed. The static modifier would counteract these properties because static methods belong to an interface itself, not to instances of the interface. This design choice emphasizes polymorphism, allowing methods in interfaces to define a contract for implementing classes to follow . By requiring non-static methods, Java ensures that inheriting interfaces can always redefine methods to empower dynamic method dispatch .

The Java Virtual Machine (JVM) plays a foundational role in Java's platform independence by acting as an intermediary between Java bytecode and the underlying operating system on which the JVM operates. Java code compiles to platform-independent bytecode which the JVM interprets or compiles at runtime, executing the same code differently tailored to a specific hardware-operating system configuration. This abstraction layer allows Java applications to run unchanged across different systems, fulfilling the "write once, run anywhere" (WORA) promise and extending Java's portability across diverse computing environments .

The "throw" keyword in Java is used for explicitly throwing an exception from a method or static block. This allows developers to manually force an exception under specified conditions. On the other hand, "throws" is a part of a method declaration and indicates that the method may throw certain exceptions during execution, effectively alerting callers of potential exceptions that must be handled. They serve different purposes in exception handling by marking where an exception originates and the methods that propagate an exception, thereby fostering error management and improving code reliability .

The "synchronized" keyword in Java is crucial for ensuring that only one thread executes a particular block of code at a time, preventing conflicting thread interactions and preserving data integrity. It can be used to synchronize methods or blocks, locking the critical section to protect shared resources. However, pitfalls include potential for deadlock if multiple threads are waiting indefinitely for locks held by each other, and reduced performance due to increased context-switching and resource locking. Furthermore, improper granularity of synchronization (locking too broadly) can significantly degrade efficiency without ensuring additional safety .

The "this" keyword in Java provides a reference to the current object whose method or constructor is being invoked. It is crucial for distinguishing instance variables from parameters or other local variables with identical names, allowing explicit reference to the object itself. By utilizing "this", developers can invoke current class's methods or constructors, improving code readability and reducing errors stemming from scoping issues. It plays a vital role in constructors to avoid redundancy when constructors invoke another constructor in the same class .

You might also like