Java Based Question for PwC
- User
Java Based Question for PwC
Assistant
- User
With Soltuions
Assistant
Java Based Question for PwC
With Soltuions
Here’s a curated list of Java-based interview questions that could be relevant for a role at PwC, covering core Java concepts, problem-solving, and frameworks like Spring. These questions test foundational knowledge, coding skills, and practical application:
Basic Syntax & OOP
== and .equals()?abstract classes and interfaces?Data Types & Memory
String, StringBuilder, and StringBuffer?Exception Handling
Exception and Error?try, catch, finally, and throw.NullPointerException? How can it be avoided?Multithreading & Concurrency
Thread and a Process?Runnable interface vs. extending the Thread class.volatile and synchronized keywords? deadlock? How can it be prevented?Collections Framework
ArrayList and LinkedList?HashMap? How does it handle collisions?Java 8+ Features
lambda expression? Give an example.Stream API and its use cases.Functional Interface?Algorithms
reverse() method.n terms.String/Array Manipulation
Recursion
Scenario-Based
Design Patterns
Spring Framework
@Component, @Service, and @Repository?Scenario-Based
Tools & Best Practices
Let me know if you’d like deeper explanations or mock answers for specific questions! 😊
Here are detailed solutions to key Java interview questions for PwC, covering core concepts, problem-solving, and frameworks:
== and .equals()Answer:
== checks object reference equality (whether two references point to the same object in memory)..equals() checks logical equality (whether two objects are "meaningfully equivalent").String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2); // false (different objects)
System.out.println(s1.equals(s2)); // true (same content)
Answer:
Answer:
String: Immutable (thread-safe but inefficient for frequent modifications).StringBuilder: Mutable, not thread-safe (faster for single-threaded use).StringBuffer: Mutable, thread-safe (slower due to synchronization).Answer:
Question: Print numbers 1–100. Replace multiples of 3 with "Fizz", 5 with "Buzz", both with "FizzBuzz".
Solution:
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0 && i % 5 == 0) {
System.out.println("FizzBuzz");
} else if (i % 3 == 0) {
System.out.println("Fizz");
} else if (i % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(i);
}
}
Question: Determine if a string reads the same backward.
Solution:
public boolean isPalindrome(String s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s.charAt(left++) != s.charAt(right--)) {
return false;
}
}
return true;
}
Question: Given an array of integers, find duplicates.
Solution:
public List<Integer> findDuplicates(int[] nums) {
Set<Integer> seen = new HashSet<>();
List<Integer> duplicates = new ArrayList<>();
for (int num : nums) {
if (!seen.add(num)) { // add returns false if already present
duplicates.add(num);
}
}
return duplicates;
}
Answer:
Use double-checked locking to ensure thread safety and lazy initialization:
public class Singleton {
private static volatile Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
Answer:
@Service
public class OrderService {
private final PaymentService paymentService;
// Spring injects PaymentService via constructor
public OrderService(PaymentService paymentService) {
this.paymentService = paymentService;
}
}
@Autowired on the constructor (optional in Spring 4.3+ if there’s only one constructor).Answer:
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users/{id}")
public ResponseEntity<User> getUser(@PathVariable Long id) {
User user = userService.findById(id);
return ResponseEntity.ok(user);
}
}
Example:
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.forEach(name -> System.out.println(name));
Example (filter and collect):
List<String> filtered = names.stream()
.filter(name -> name.startsWith("A"))
.collect(Collectors.toList());
StringBuilder for concatenation, prefer constructor injection, and avoid == for object comparison.HashMap vs. ConcurrentHashMap for thread safety.Let me know if you’d like more details on any topic! 😊