Functions / Methods Question Bank
Showing 49 questions of 49 with verified answers
(Code-Tracing) Trace the value of s1 and s2 in this Java/C# snippet: String s1 = "Hello"; String s2 = s1; s1 = s1 + " World"; print(s2);
(Scenario-Based) You need to build a long HTML report by joining 10,000 small strings together in a loop. Which approach is the most efficient?
(Debugging) A developer is checking if a user entered the correct password: if (input_pass == "admin123") { ... } Why is this logic flawed in languages like Java?
(Conceptual) In UTF-8 encoding, how many bytes are typically used to represent a standard English character versus an Emoji?
(Code-Tracing) Trace the output of this Python-style snippet: text = "Graduate" print(text[1:4])
(Scenario-Based) You need to validate if a string is a valid email address. Which tool is best for this pattern matching task?
(Conceptual) What is the result of the "Interning" process in string management?
(Code-Tracing) What is the output? String val = "10"; val += 20; print(val);
(Scenario-Based) A developer is processing a CSV file. For each line, they need to extract the "Name" which is between the first and second comma. Which function combination is most logical?
(Conceptual) Which of the following is true about a "Null" string versus an "Empty" string?
(Calculation) If an algorithm to reverse a string of length N uses a simple loop to swap characters, what is its Time Complexity?
(Debugging) Look at this Regex: ^\d{5}$. Which string will it match?
(Conceptual) What is the purpose of the trim() function in string processing?
(Scenario-Based) You have a multithreaded application where 10 threads are all appending data to a shared log string simultaneously. Which class should you use in Java to ensure no data is corrupted?
(Conceptual) Why is accessing an element in a standard Array by its index considered a Constant Time O(1) operation? • • • •
(Scenario-Based) You are given a list of 1 million student IDs, and you need to find all the unique IDs in the list. Which data structure is the most efficient choice for this task?
(Code-Tracing) Trace the output of this logic involving a Dynamic Array (ArrayList): list = [10, 20, 30] list.add(1, 15) list.remove(2) print(list)
(Calculation) In a 2D integer array matrix[5][10], how many total elements are stored in memory, and what is the index of the very last element?
(Comparison) What is a major performance disadvantage of a Linked List compared to an Array?
(Debugging) A developer is iterating through a List and removing items that meet a condition: for (Item x : myList) { if (x.isExpired()) myList.remove(x); } What is the likely outcome in Java or C#?
(Conceptual) Which data structure follows the LIFO (Last-In-First-Out) principle?
(Scenario-Based) You are building an "Undo" feature for a text editor. Every time the user types, the change is saved. When they press "Undo," the most recent change is reversed. Which collection is most appropriate?
(Code-Tracing) What is the output? int[] arr = new int[3]; print(arr[0]); (In a language like Java where arrays are zero-initialized)
(Comparison) What is the Time Complexity of looking up a value by its Key in a HashMap (on average)?
(Scenario-Based) You are managing a printer queue where the first document sent must be the first one printed. Which data structure is best?
(Conceptual) What happens when a Hash Collision occurs in a HashMap?
(Code-Tracing) Trace the value of result: Stack s = new Stack(); s.push(10); s.push(20); s.pop(); s.push(30); result = s.peek();
(Calculation) What is the Time Complexity of inserting a new element at the **index 0** of an ArrayList of size N?
(Scenario-Based) A developer needs to store pairs of "Country Name" and "Capital City." They need to be able to quickly find the capital given a country name. Which collection is best?
(Conceptual) What is the difference between a Capacity and Size in a dynamic array like ArrayList?
(Debugging) Why is using an Array usually safer for performance-critical code than an ArrayList of Objects?
(Scenario-Based) You need to implement a "Priority Task List" where the task with the highest importance is always finished first, regardless of when it was added. Which structure is best?
(Code-Tracing) Trace the output: set = new HashSet(); set.add("A"); set.add("B"); set.add("A"); print(set.size());
(Conceptual) What is the "Load Factor" of a HashMap?
(Conceptual) Which OOP principle is primarily concerned with reducing "Tight Coupling" between different parts of a system?
(Scenario-Based) You have a class Shape with a method draw(). You have subclasses Circle and Square that provide their own versions of draw(). At runtime, the program decides which draw() to call based on the object type. This is:
(Code-Tracing) Look at this Java/C# snippet. What is the output? class Parent { void show() { print("Parent"); } } class Child extends Parent { void show() { print("Child"); } } Parent obj = new Child(); obj.show();
(Conceptual) What is the "Diamond Problem" in Object-Oriented Programming?
(Scenario-Based) A developer wants to create a class Vehicle that can never be instantiated on its own but provides a common startEngine() method for Car and Truck. What should Vehicle be?
(Code-Tracing) Trace the value of count: class Counter { static int count = 0; Counter() { count++; } } Counter c1 = new Counter(); Counter c2 = new Counter(); Counter c3 = new Counter(); print(Counter.count);
(Comparison) What is a key difference between an Interface and an Abstract Class in modern Java (post-Java 8)?
(Conceptual) Which of the following best describes Composition over Inheritance?
(Debugging) Why will the following code fail to compile? class Test { int x = 10; static void display() { print(x); } }
(Conceptual) What is the purpose of a Constructor?
(Scenario-Based) You are designing a PaymentSystem. You have different types of payments: CreditCard, PayPal, and Crypto. They all must have a process() method. What is the best way to enforce this?
(Code-Tracing) What is Method Overloading?
(Conceptual) What is the meaning of the final keyword (or sealed in C#) when applied to a **Class**?
(Scenario-Based) In a User class, the password field is set to private and can only be changed via a setPassword() method that checks for a minimum length. Which OOP principle is being applied?
In object-oriented programming, which principle allows a derived class method to replace the implementation of a base class virtual method at runtime?