java choice
java choice
• A) Encapsulation
• B) Inheritance
• C) Polymorphism
• D) Compilation
Answer: D
3. What is encapsulation?
• A) new
• B) class
• C) create
• D) object
Answer: B
• A) Method Overloading
• B) Method Overriding
• C) Both A and B
8. Which of the following access modifiers allows access only within the same
package?
• A) private
• B) protected
• C) public
• D) default
Answer: D
• A) Abstract Classes
• B) Interfaces
• C) Both A and B
• A) abstract
• B) void
• C) interface
• D) new
Answer: A
16. Which of the following allows a class to inherit fields and methods from
another class?
• A) Interface
• B) Abstract Class
• C) Inheritance
• D) Encapsulation
Answer: C
18. Which feature allows you to define multiple methods with the same name but
different parameters?
• B) Encapsulation
• C) Inheritance
• D) Method Overloading
Answer: D
• A) Encapsulation
• B) Inheritance
• C) Compilation
• D) Polymorphism
Answer: C
22. In Java, which keyword is used to indicate that a class cannot be subclassed?
• A) final
• B) static
• C) private
• D) abstract
Answer: A
23. What will happen if you do not provide any constructor in your class?
25. Which of the following allows you to implement multiple inheritance in Java?
• A) Classes
• B) Interfaces
• C) Abstract Classes
• B) It must have the same name and parameters as the method in superclass.
29. What happens if a subclass does not implement an abstract method from its
superclass?
• A) final
• B) static
• C) private
• D) protected
Answer: A
• A) inherit
• B) extends
• C) implements
• D) super
Answer: B
35. Which method can be used to convert an object into a string representation in
Java?
• A) print()
• B) display()
• C) toString()
• D) valueOf()
Answer: C
• A) this
• B) super
• C) parent
• D) base
Answer: B
• A)
Polymorphism allows methods to do different things based on the object it is
acting upon.
• B)
It can be achieved through method overloading and overriding.
• C)
Polymorphism can only occur with inherited methods.
• D)
Polymorphism enhances flexibility and maintainability in code design.
Answer: C
• A)
The type of object is determined at compile time.
• B)
The type of object is determined at runtime.
• C)
The type of method is fixed at compile time.
• D)
None of the above.
Answer: B
• A)
Composition
• B)
Aggregation
• C)
Inheritance
• D)
Encapsulation
Answer: C
42. In Java, what does 'static' mean when applied to variables or methods?
• A)
They belong to instances of a class rather than the class itself.
• B)
They are shared among all instances of a class.
• C)
They cannot be accessed outside their own class.
• D)
They can only be accessed by subclasses.
Answer: B
43. Which feature of OOP allows for defining a common interface for different
data types?
• A)
Encapsulation
• B)
Abstraction
• C)
Inheritance
• D)
Polymorphism
Answer: B
• A)
It can be accessed from anywhere within the program.
• B)
It can only be accessed within its own class.
• C)
It can be accessed by subclasses only.
• D)
It can be accessed by any other classes in the same package.
Answer: B
• A)
A constructor must have a return type.
• B)
A constructor name must match its class name exactly, including case sensitivity.
• C)
Constructors can be static methods.
• D)
Constructors cannot be overloaded.
Answer: B
• A)
The variable can never be changed after initialization.
• B)
The variable can only be changed once during its lifetime.
• C)
The variable can be changed but not reassigned entirely.
• D)
None of these statements are correct.
Answer: A
• A)
Single Inheritance
• B)
Multiple Inheritance
• C)
Multilevel Inheritance
• D)
Java does not support multiple inheritance through classes; it supports it through
interfaces only.
Answer: D
48. What will happen if you try to access a private member from outside its class?
• A)
It will compile successfully but throw an error at runtime.
• B)
It will not compile; an access error will occur at compile time.
• C)
It will compile but give incorrect results at runtime.
• D)
There are no restrictions on accessing private members; they are public by default.
Answer: B
• A)
It represents a strong relationship between objects where one cannot exist without
another.
• B)
It represents a weak relationship where one object contains another but both can
exist independently.
• C)
It is another term for inheritance in OOP design.
• D)
None of these statements are correct.
Answer: B
• A)
An interface cannot contain any methods while an abstract class can have both
abstract and concrete methods.
• B)
An abstract class cannot implement interfaces while interfaces can implement
abstract classes.
• C)
Both interfaces and abstract classes can have method implementations since Java
8.
• D)
Interfaces can have constructors while abstract classes cannot have constructors at
all.
Answer: C
• A)
Clients should not be forced to depend on methods they do not use; hence, many
client-specific interfaces are better than one general-purpose interface.
• B)
Interfaces should contain all methods required by clients regardless of their usage
frequency or relevance.
• C)
Interfaces should always inherit from other interfaces, ensuring that all
functionality remains intact across implementations.
• D)
None of these statements are correct.
Answer: A
• A)
Inner classes cannot access members (including private ones).
• B)
Inner classes are declared outside any other class and cannot access outer class
members directly.
• C)
Inner classes can access members (including private ones).
• D)
Inner classes must always be static and cannot have non-static members
themselves.
Answer: C
53. Which method allows you to compare two objects for equality in Java?
• A)
compareTo()
• B)
equals()
• C)
== operator
• D)
compare()
Answer: B
• A)
It calls another constructor from within that same constructor's class context,
providing additional initialization logic before executing its body.
• B)
It calls the superclass's constructor before executing any code inside that
constructor's body, allowing proper initialization order between parent and child
classes' attributes or behaviors as needed based on constructor parameters passed
along if applicable too!
• C)
It throws an error since constructors cannot call super().
• D)
None of these statements are correct!
Answer: B
• A)
The new interface inherits all methods from its parent interface, thereby allowing
any implementing class access as well!
• B)
The new interface overrides existing methods from its parent interface while
adding new ones simultaneously!
• C)
The new interface must provide implementations for all inherited methods
immediately upon declaration!
• D)
None of these statements are correct!
Answer: A
• A)
By declaring variables as public so they are accessible anywhere!
• B)
By using private variables along with public getter/setter methods!
• C)
By using static methods only!
• D)
None of these statements are correct!
Answer: B
• A)
Abstraction
• B)
Encapsulation
• C)
Polymorphism
• D)
Inheritance
Answer: C
• A)
It will compile successfully but behave differently at runtime depending on which
reference type invokes it!
• B)
• C)
It will override without any issues since static binding occurs at compile time
instead!
• D)
None of these statements are correct!
Answer: C
65. What happens when you attempt to create an instance of an interface directly?
–
A)
It will compile successfully but throw an error at runtime!
–
B)
It will compile successfully and allow instantiation without issues!
–
C)
It will not compile since interfaces cannot be instantiated directly!
–
D)
None of these statements are correct!
Answer: C
Sure! Here are the multiple-choice questions with their answers and explanations:
• A) class MyClass { }
• B) MyClass class { }
• C) class: MyClass { }
• Explanation: In Java, classes are defined using the class keyword followed by
the class name and curly braces that contain the class body.
• A) int
• B) char
• C) String
• D) boolean
• Answer: C) String
• Explanation: In Java, primitive data types include int, char, boolean, etc. String
is an object, not a primitive type.
• B) 6
• C) 0
• D) Compilation error
• Answer: A) 5
• Explanation: The x++ operation returns the current value of x (which is 5) and
then increments it. Therefore, the output is 5.
▎Control Structures
• A) continue
• B) break
• C) exit
• D) return
• Answer: B) break
• B) 0 1 2
• C) 0 1 2 3
• D) 1 2
• Answer: B) 0 1 2
• Explanation: The loop starts at i = 0 and runs while i < 3. It prints values of i
from 0 to 2.
• A) implements
• B) inherits
• C) extends
• D) super
• Answer: C) extends
• Explanation: The extends keyword is used in Java to indicate that one class is
inheriting from another class.
▎Exception Handling
• Explanation: The finally block is always executed after the try-catch blocks,
regardless of whether an exception was thrown or caught.
▎Collections Framework
11. Which of the following classes implements a LIFO (Last In First Out) data
structure?
• A) ArrayList
• B) LinkedList
• C) Stack
• D) HashMap
• Answer: C) Stack
• Explanation: The Stack class in Java implements a Last In First Out (LIFO)
data structure, meaning that the last element added is the first one to be removed.
• Explanation: The add() method in a List interface adds an element to the end of
the list.
▎Multithreading
• A) run()
• B) execute()
• C) start()
• D) begin()
• Answer: C) start()
• D) To terminate a thread.
▎Miscellaneous
15. What does the keyword final mean when applied to a variable?
▎Advanced Topics
• A) Hel
• B) He
• C) Hello
• D) H
• Answer: B) He
• A) Platform independence
• B) High performance
• C) Low security
• D) Multithreading
• Explanation: The @Override annotation informs the compiler that the method
is intended to override a method in its superclass, helping catch errors if no such
method exists.
---
▎String Handling
• A) 4
• B) 5
• C) 6
• D) Null
Answer: B) 5
Explanation: The length() method returns the number of characters in the string
"hello", which is 5.
---
• A) indexOf()
• B) findIndex()
• C) charIndex()
• D) locate()
Answer: A) indexOf()
Explanation: The indexOf() method returns the index of the first occurrence of a
specified character in a string.
---
▎File Handling
3. Which class is used to create a file in Java?
• A) FileWriter
• B) File
• C) FileOutputStream
• D) FileCreator
Answer: B) File
Explanation: The File class is used to create, delete, and manipulate files and
directories.
---
• A) true
• B) false
• C) Exception
• D) null
Answer: A) true
Explanation: The canRead() method checks if the file can be read, returning true
if it exists and is readable.
---
▎Collections
• A) List
• B) Set
• C) Queue
• D) Map
Answer: B) Set
Explanation: The Set interface in Java does not allow duplicate elements.
---
• A) HashSet
• B) ArrayList
• C) HashMap
• D) TreeSet
Answer: B) ArrayList
Explanation: ArrayList is a resizable array implementation of the List interface.
---
▎Object-Oriented Programming
• A) inherit
• B) extends
• C) implements
• D) derives
Answer: B) extends
Explanation: The extends keyword is used to inherit a class in Java.
---
8. Which of the following is a feature of encapsulation?
• A) Data hiding
• B) Inheritance
• C) Polymorphism
• D) Abstraction
---
▎Exception Handling
• A) NullPointerException
• B) ArrayIndexOutOfBoundsException
• C) ClassCastException
• D) ArithmeticException
Answer: B) ArrayIndexOutOfBoundsException
Explanation: This exception is thrown when an attempt is made to access an
array with an index that is out of bounds.
---
• A) try block
• B) catch block
• C) finally block
• D) all of the above
Answer: D) all of the above
Explanation: The try block contains code that may throw an exception, the catch
block handles it, and the finally block executes regardless of whether an exception
occurred.
---
▎Miscellaneous Concepts
• A) const
• B) final
• C) static
• D) immutable
Answer: B) final
Explanation: The final keyword is used to declare constants in Java.
---
• A) Instance variable
• B) Class variable
• C) Local variable
• D) Global variable
---
▎Multithreading
• A) run()
• B) start()
• C) execute()
• D) begin()
Answer: B) start()
Explanation: The start() method is used to initiate thread execution.
---
---
▎Networking
• A) SocketConnection
• B) Socket
• C) ServerSocket
• D) DatagramSocket
Answer: B) Socket
Explanation: The Socket class is used for creating client socket connections in
Java.
---
• A) FTP
• B) HTTP/HTTPS
• C) SMTP
• D) SNMP
Answer: B) HTTP/HTTPS
Explanation: HTTP (Hypertext Transfer Protocol) and HTTPS (HTTP Secure)
are standard protocols for web communication.
---
▎Database Connectivity
• A) java.sql
• B) java.dbc
• C) java.database
• D) java.jdbc
Answer: A) java.sql
Explanation: The java.sql package contains classes and interfaces for JDBC
(Java Database Connectivity).
---
▎Annotations
• A) @Override
• B) @OverrideMethod
• C) @OverrideFunction
• D) @MethodOverride
Answer: A) @Override
Explanation: The @Override annotation indicates that a method is intended to
override a method declared in a superclass.
---
▎Lambda Expressions
---
▎Streams
• A) filter()
• B) map()
• C) collect()
• D) reduce()
Answer: A) filter()
Explanation: The filter() method allows you to specify a condition that elements
must meet to be included in the resulting stream.
---
▎Generics
---
▎Interfaces
Answer: A) implements
Explanation: The implements keyword is used by a class to inherit from an
interface.
---
▎Access Modifiers
23. Which access modifier allows access only within its own package?
• A) public
• B) private
• C) protected
• D) default (package-private)
---
▎Static Methods
• A) Yes, always
• B) No, they cannot access instance variables directly without an object
reference.
• C) Yes, but only if declared as final.
• D) No, static methods cannot access any variables.
Answer: B) No, they cannot access instance variables directly without an object
reference.
Explanation: Static methods belong to the class and do not have access to
instance variables unless they create an instance of the class.
---
▎Constructors
---
▎Enums
• Explanation: Enums are defined using the enum keyword followed by the
name and its constants enclosed in braces.
---
▎HashMap
• A ) getValue()
• B ) fetch()
• C ) get()
• D ) retrieve()
• Answer: C ) get()
• Explanation: The get() method retrieves the value associated with a specified
key from a HashMap.
---
▎StringBuilder
• A ) String
• B ) StringBuilder
• D ) Neither is mutable
• Answer: B ) StringBuilder
---
▎Threads
• D ) It prioritizes threads.
• Explanation: The synchronized keyword ensures that only one thread can
access a block of code or object at any given time.
---
▎Reflection
---
▎Annotations
• A ) @NotOverridden
• B ) @Final
• C ) @Static
• D ) @Abstract
• Answer: B ) @Final
---
▎Serialization
• A ) Serializable interface
• B ) Cloneable interface
• C ) Externalizable interface
• D ) Both A and C
---
▎Method Overloading
---
▎Method Overriding
▎Inner Classes
• Explanation: Inner classes can access all members (including private members
and methods).
---
▎Static Blocks
• Explanation: Static blocks are executed when the class is loaded into memory.
---
▎Default Methods
---
▎Optional Class
---
▎Streams API
– A ) map()
– B ) filter()
– C ) sorted()
– D ) all of these modify original Stream
---
---