Showing 581–590 of 654 questions
Which design pattern is used when you want to ensure a class has only one instance?
A
Factory Pattern
B
Singleton Pattern
C
Builder Pattern
D
Observer Pattern
Correct Answer:
B. Singleton Pattern
Explanation:
Singleton Pattern restricts a class to have only one instance and provides a global access point to it.
What will be the output?
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
list.remove(1);
System.out.println(list);
A
[1, 3]
B
[1, 2, 3]
C
[2, 3]
D
[1, 2]
Correct Answer:
A. [1, 3]
Explanation:
list.remove(1) removes the element at index 1 (which is 2), leaving [1, 3].
What is the difference between ArrayList and LinkedList in Java?
A
ArrayList is faster for random access
B
LinkedList is faster for insertions/deletions in middle
C
Both are correct
D
No difference
Correct Answer:
C. Both are correct
Explanation:
ArrayList has O(1) random access but O(n) insertion/deletion. LinkedList has O(n) access but O(1) insertion/deletion.
Which of the following is NOT a principle of object-oriented programming?
A
Inheritance
B
Encapsulation
C
Polymorphism
D
Materialization
Correct Answer:
D. Materialization
Explanation:
The four OOP principles are Inheritance, Encapsulation, Polymorphism, and Abstraction. 'Materialization' is not an OOP principle.
What is the purpose of the 'try-catch' block in Java?
A
To improve code performance
B
To handle exceptions gracefully
C
To declare variables
D
To create loops
Correct Answer:
B. To handle exceptions gracefully
Explanation:
try-catch blocks allow you to handle exceptions without crashing the program, providing graceful error handling.
If a HashMap has 1000 entries and the load factor is 0.75, at what capacity will it resize?
A
750
B
1000
C
1333
D
Cannot be determined
Explanation:
Resize occurs when entries exceed capacity × load factor. If 1000 entries at 0.75 load factor, capacity = 1000/0.75 = 1333
What is the time complexity of accessing an element in a balanced Binary Search Tree?
A
O(1)
B
O(log n)
C
O(n)
D
O(n²)
Correct Answer:
B. O(log n)
Explanation:
In a balanced BST, the height is log n, so searching takes O(log n) time.
Which protocol is used for secure data transmission over the internet?
A
HTTP
B
HTTPS
C
FTP
D
SMTP
Explanation:
HTTPS (HTTP Secure) encrypts data in transit using SSL/TLS. Other protocols lack this encryption.
What does the SELECT * query do in SQL?
A
Selects the first row
B
Selects all columns from all rows
C
Selects only the primary key
D
Deletes all data
Correct Answer:
B. Selects all columns from all rows
Explanation:
SELECT * retrieves all columns and rows from a table. * is a wildcard meaning 'all columns'.
A train travels from City A to City B at 60 km/h and returns from B to A at 40 km/h. If the total journey time is 10 hours, what is the distance between City A and City B?
A
240 km
B
300 km
C
280 km
D
320 km
Correct Answer:
A. 240 km
Explanation:
Let distance = d km. Time A to B = d/60 hours, Time B to A = d/40 hours. Total time: d/60 + d/40 = 10. LCM(60,40)=120, so (2d + 3d)/120 = 10, giving 5d/120 = 10, thus d = 240 km.