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

mcq practice oop

Uploaded by

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

mcq practice oop

Uploaded by

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

1. What symbol is used for string concatenation in Java?

- a) &
- b) +
- c) *
- d) $

2. Which escape sequence is used to insert a newline?


- a) \t
- b) \n
3. What type of data does the `boolean` primitive store in Java?
- a) Character values
- b) Integer values
- c) True/false values
- d) String values

4. Which method is used to print without moving to a new line?


- a) println
- b) print
- c) printf
- d) display

5. What is the output of the following code snippet?


```java
System.out.println("X: " + 25);
```
- a) X:
- b) X: 25
- c) X: + 25
- d) Error

6. What happens when you divide two integers in Java?


- a) Returns a floating-point result
- b) Returns an integer result
- c) Compilation error
- d) Returns a rounded integer result

7. Which operator has the highest precedence in Java?


- a) +
- b) *
- c) =
- d) ()

8. What does the `%` operator in Java return?


- a) The quotient
- b) The remainder
- c) The sum
- d) The product

9. What will be the output of the following?


```java
System.out.println("Roses are red,\nViolets are blue");
```
- a) Roses are red, Violets are blue
- b) Roses are red,\nViolets are blue
- c) Roses are red,
Violets are blue
- d) None of the above

10. Which of the following is not a primitive data type in Java?


- a) int
- b) double
- c) boolean
- d) String

11. Which method can be used to find the length of a string?


- a) length()
- b) size()
- c) getLength()
- d) len()

12. What does the `Random` class belong to?


- a) java.lang
- b) java.util
- c) java.math
- d) java.text

13. How is an object created in Java?


- a) Using the new keyword
- b) Direct assignment
- c) Using a constructor
- d) Both a and c

14. What is returned by `Math.sqrt(16)`?


- a) 2
- b) 4
- c) 8
- d) 16

15. Which of these is not a valid wrapper class in Java?


- a) Integer
- b) Double
- c) Character
- d) String

16. What is the purpose of the `toString` method in Java?


- a) To concatenate strings
- b) To convert an object to a string representation
- c) To create a new string object
- d) To compare strings

17. Enumerated types in Java are:


- a) A list of constant values
- b) A type of class
- c) Immutable
- d) All of the above

18. What does `System.out.println()` return?


- a) The printed text
- b) Void
- c) An integer
- d) None of the above

19. What is the range of values for `Random.nextInt(50)`?


- a) 0 to 50
- b) 1 to 50
- c) 0 to 49
- d) -50 to 50

20. What is the output of the following?


```java
String s = "Java";
System.out.println(s.toUpperCase());
```
- a) java
- b) JAVA
- c) Error
- d) null

21. What is encapsulation in Java?


- a) Bundling of data and methods
- b) Hiding implementation details
- c) Both a and b
- d) None of the above

22. What is the purpose of a constructor?


- a) To initialize objects
- b) To destroy objects
- c) To modify objects
- d) None of the above

23. Which keyword is used to declare a constant in Java?


- a) static
- b) final
- c) const
- d) constant

24. What is an instance variable in Java?


- a) A variable defined inside a method
- b) A variable defined at the class level
- c) A global variable
- d) None of the above
25. How is memory allocated for instance variables?
- a) During class loading
- b) When an object is created
- c) During method execution
- d) None of the above

26. What does the `this` keyword in Java refer to?


- a) The current object instance
- b) The superclass of the object
- c) A static reference
- d) None of the above

27. Which method is automatically invoked when an object is created?


- a) Main method
- b) Constructor
- c) toString
- d) finalize

28. What is the primary purpose of accessor methods?


- a) To modify instance variables
- b) To retrieve instance variables
- c) To initialize instance variables
- d) None of the above

29. In Java, encapsulation is primarily achieved using:


- a) Interfaces
- b) Inheritance
- c) Access modifiers
- d) Annotations
30. Which of the following is true about mutator methods?
- a) They must always return a value
- b) They modify the state of an object
- c) They cannot be private
- d) They are only used for constants

31. How many constructors can a Java class have?


- a) One
- b) Two
- c) As many as required
- d) None

32. What does the `super` keyword allow in a constructor?


- a) To invoke a method of the current class
- b) To invoke a superclass constructor
- c) To reference a static method
- d) None of the above

33. Which class method in Java cannot be overridden?


- a) Private methods
- b) Static methods
- c) Final methods
- d) All of the above

34. What is the return type of a constructor in Java?


- a) void
- b) The class name
- c) None
- d) int

35. What is the default value of an uninitialized object reference?


- a) null
- b) 0
- c) undefined
- d) None

36. What is the purpose of the `toString` method?


- a) Modify an object
- b) Return a string representation of an object
- c) Compare objects
- d) None of the above

37. What is polymorphism in Java?


- a) Ability of a method to perform different tasks based on the object calling it
- b) Inheriting methods from a superclass
- c) Overriding static methods
- d) None of the above

38. Which of the following is true for method overloading?


- a) Methods must have the same return type
- b) Methods must have different parameter lists
- c) Methods must be in different classes
- d) Methods must be abstract

39. What is the use of the `final` keyword with a method?


- a) It prevents the method from being called
- b) It prevents the method from being overridden
- c) It makes the method abstract
- d) It makes the method static

40. In Java, which access modifier makes a member accessible only within the
same package?
- a) private
- b) public
- c) protected
- d) default

41. Which of the following allows an object to be self-governing?


- a) Polymorphism
- b) Encapsulation
- c) Inheritance
- d) None of the above

42. When is the `finalize()` method called?


- a) When the garbage collector destroys an object
- b) When a program ends
- c) When a thread starts
- d) None of the above

43. What is a constructor that takes no arguments called?


- a) Default constructor
- b) Overloaded constructor
- c) Parameterized constructor
- d) None of the above

44. What does the `static` keyword imply when applied to a method?
- a) The method belongs to an object
- b) The method belongs to the class
- c) The method cannot be overridden
- d) The method is private

45. What is inheritance in Java?


- a) Reusing methods within the same class
- b) Creating new classes from existing classes
- c) Hiding data members of a class
- d) None of the above

46. Which keyword is used to inherit a class in Java?


- a) implements
- b) extends
- c) inherits
- d) super

47. What is the base class in inheritance often called?


- a) Subclass
- b) Child class
- c) Superclass
- d) Derived class

48. Can constructors be inherited in Java?


- a) Yes
- b) No

49. Which method is used to call a superclass’s constructor?


- a) super()
- b) this()
- c) base()
- d) None of the above

50. What type of inheritance is not supported by Java?


- a) Single
- b) Multiple
- c) Multilevel
- d) Hierarchical

51. Which keyword is used to prevent a class from being inherited?


- a) abstract
- b) static
- c) final
- d) sealed

52. What is method overriding in Java?


- a) Redefining a method in the child class with the same name and parameters
as in the parent class
- b) Using multiple methods with the same name but different parameters
- c) Making methods private
- d) None of the above

53. What is an interface in Java?


- a) A class with abstract methods
- b) A collection of abstract methods
- c) A class that cannot be instantiated
- d) None of the above

54. Which keyword is used to implement an interface?


- a) extends
- b) inherits
- c) implements
- d) interface

55. Can an interface extend another interface?


- a) Yes
- b) No
56. Which of these statements is true for abstract classes?
- a) They cannot have constructors
- b) They can contain both concrete and abstract methods
- c) They must implement all methods from interfaces
- d) None of the above

57. Can an abstract class be instantiated?


- a) Yes
- b) No

58. What happens if a class implements an interface but does not provide
implementations for all methods?
- a) It throws a runtime error
- b) The class must be declared abstract
- c) It compiles successfully
- d) None of the above

59. What is the purpose of the `default` keyword in interfaces?


- a) To allow concrete methods in interfaces
- b) To make methods private
- c) To provide a default constructor
- d) None of the above

60. What is a wrapper class in Java?


- a) A class that wraps another object
- b) A class that converts primitives into objects
- c) A class that hides implementation details
- d) None of the above
61. Which keyword is used to declare a constant in an interface?
- a) static
- b) final
- c) abstract
- d) None of the above

62. Can an interface contain instance variables?


- a) Yes
- b) No

63. Which of these is true for methods declared in an interface?


- a) They must be static
- b) They must be public and abstract
- c) They must be private
- d) None of the above

64. What is the difference between abstract classes and interfaces?


- a) Abstract classes can have instance variables, interfaces cannot
- b) Interfaces support multiple inheritance; abstract classes do not
- c) Both a and b
- d) None of the above

65. What is an exception in Java?


- a) A compilation error
- b) An event that disrupts program execution
- c) A logical error
- d) None of the above
66. Which keyword is used to handle exceptions in Java?
- a) catch
- b) try
- c) throw
- d) finally

67. What happens when an exception is not caught in Java?


- a) The program terminates
- b) The exception is ignored
- c) The program continues to execute
- d) None of the above

68. Which keyword is used to manually throw an exception?


- a) throw
- b) throws
- c) new
- d) None of the above

69. Which block of code is always executed, regardless of whether an exception


occurs?
- a) try
- b) catch
- c) finally
- d) None of the above

70. What is a checked exception in Java?


- a) An exception that is ignored during compilation
- b) An exception that must be handled or declared in the method signature
- c) An exception related to runtime errors
- d) None of the above
71. Which of these is not a runtime exception?
- a) NullPointerException
- b) ArithmeticException
- c) IOException
- d) IndexOutOfBoundsException

72. What is the purpose of generics in Java?


- a) To improve runtime performance
- b) To allow type safety at compile time
- c) To allow multiple inheritance
- d) None of the above

73. Which of these is a valid declaration of a generic class?


- a) class MyClass<T>
- b) class MyClass<generic>
- c) class MyClass[T]
- d) None of the above

74. What does the `ArrayList` class implement?


- a) List
- b) Set
- c) Queue
- d) Map

75. Which method is used to add an element to an ArrayList?


- a) insert()
- b) append()
- c) add()
- d) put()

76. What is the default initial capacity of an ArrayList?


- a) 5
- b) 10
- c) 15
- d) 20

77. What is the difference between HashSet and TreeSet?


- a) HashSet is unordered, TreeSet maintains order
- b) TreeSet is faster than HashSet
- c) HashSet allows duplicates, TreeSet does not
- d) None of the above

78. Which interface does HashMap implement?


- a) Set
- b) Map
- c) List
- d) Collection

79. Can a HashMap have duplicate keys?


- a) Yes
- b) No

**Chapter 9: Threads and Concurrency**

80. What is a thread in Java?


- a) A lightweight process
- b) A program
- c) A type of data structure
- d) None of the above

81. Which method is used to start a thread?


- a) run()
- b) start()
- c) execute()
- d) None of the above

82. What does the `synchronized` keyword do?


- a) Makes a method thread-safe
- b) Increases thread priority
- c) Stops a thread
- d) None of the above

83. What is the state of a thread when it is executing?


- a) Waiting
- b) Running
- c) Blocked
- d) Terminated

84. Which method is used to put a thread to sleep?


- a) wait()
- b) sleep()
- c) pause()
- d) None of the above

85. Can multiple threads access the same synchronized method?


- a) Yes
- b) No

**Chapter 10: File I/O and Streams**

86. What is a stream in Java?


- a) A sequence of data
- b) A class for handling threads
- c) A type of collection
- d) None of the above

87. Which class is used for reading text from a file in Java?
- a) FileReader
- b) FileInputStream
- c) BufferedReader
- d) Scanner

88. What does the `File` class in Java represent?


- a) A file or directory path
- b) An open file
- c) A file stream
- d) None of the above

89. Which method is used to check if a file exists?


- a) exists()
- b) isFile()
- c) isPresent()
- d) None of the above

90. What is the difference between FileReader and FileInputStream?


- a) FileReader is for text files, FileInputStream is for binary files
- b) FileReader is faster than FileInputStream
- c) FileInputStream cannot read text files
- d) None of the above

91. Which of these is a valid constructor of the `PrintWriter` class?


- a) PrintWriter(OutputStream)
- b) PrintWriter(File)
- c) PrintWriter(String fileName)
- d) All of the above

92. Which package is primarily used for GUI programming in Java?


- a) java.io
- b) java.util
- c) javax.swing
- d) java.net

93. What is a JFrame in Java?


- a) A container for holding GUI components
- b) A layout manager
- c) A method to create a thread
- d) None of the above

94. Which method is used to make a JFrame visible?


- a) show()
- b) setVisible(true)
- c) render()
- d) display()

95. What is the purpose of the `FlowLayout` manager?


- a) To arrange components in a row
- b) To arrange components in a grid
- c) To arrange components in a single row or column
- d) To stack components on top of each other

96. Which of these is a valid method to add a component to a JFrame?


- a) addComponent()
- b) add()
- c) insertComponent()
- d) attach()
97. What is the default layout manager for a JPanel?
- a) FlowLayout
- b) BorderLayout
- c) GridLayout
- d) None of the above

98. Which event listener is used for handling button clicks?


- a) ActionListener
- b) ItemListener
- c) KeyListener
- d) MouseListener

99. What is the use of the `setSize()` method in a JFrame?


- a) To set the size of the window
- b) To add components
- c) To set the visibility
- d) None of the above

100. What is the purpose of the `pack()` method in GUI programming?


- a) To automatically set the size of the window to fit components
- b) To set the layout manager
- c) To compile the program
- d) None of the above

**Chapter 12: Applets**


101. What is an applet in Java?
- a) A standalone application
- b) A small program that runs in a web browser
- c) A type of thread
- d) None of the above

102. Which method is used to initialize an applet?


- a) start()
- b) init()
- c) run()
- d) main()

103. Which method is used to draw graphics in an applet?


- a) paintComponent()
- b) paint()
- c) render()
- d) display()

104. What does the `Applet` class inherit from?


- a) Object
- b) Frame
- c) Panel
- d) None of the above

105. Can applets access the local file system by default?


- a) Yes
- b) No
106. What is the purpose of the `update()` method in an applet?
- a) To redraw the entire applet
- b) To close the applet
- c) To load an applet
- d) None of the above

**END OF MCQs**

You might also like