Independent Preparation Bank for Pakistani Computing Graduates • 100% Free • No Signup
NSCTPrep.
Programming (C++ / Java / Python)Topic #3 of 549 MCQs Available

Functions / Methods

Comprehensive question bank, step-by-step rationales, and dedicated timed quiz for Functions / Methods.

Launch Quiz Mode ↗

Functions / Methods Question Bank

Showing 49 questions of 49 with verified answers

2Multiple Choice

(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);

3Multiple Choice

(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?

4Multiple Choice

(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?

5Multiple Choice

(Conceptual) In UTF-8 encoding, how many bytes are typically used to represent a standard English character versus an Emoji?

6Multiple Choice

(Code-Tracing) Trace the output of this Python-style snippet: text = "Graduate" print(text[1:4])

7Multiple Choice

(Scenario-Based) You need to validate if a string is a valid email address. Which tool is best for this pattern matching task?

8Multiple Choice

(Conceptual) What is the result of the "Interning" process in string management?

9Multiple Choice

(Code-Tracing) What is the output? String val = "10"; val += 20; print(val);

10Multiple Choice

(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?

11Multiple Choice

(Conceptual) Which of the following is true about a "Null" string versus an "Empty" string?

12Multiple Choice

(Calculation) If an algorithm to reverse a string of length N uses a simple loop to swap characters, what is its Time Complexity?

13Multiple Choice

(Debugging) Look at this Regex: ^\d{5}$. Which string will it match?

14Multiple Choice

(Conceptual) What is the purpose of the trim() function in string processing?

15Multiple Choice

(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?

1Multiple Choice

(Conceptual) Why is accessing an element in a standard Array by its index considered a Constant Time O(1) operation? • • • •

2Multiple Choice

(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?

3Multiple Choice

(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)

4Multiple Choice

(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?

5Multiple Choice

(Comparison) What is a major performance disadvantage of a Linked List compared to an Array?

6Multiple Choice

(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#?

7Multiple Choice

(Conceptual) Which data structure follows the LIFO (Last-In-First-Out) principle?

8Multiple Choice

(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?

9Multiple Choice

(Code-Tracing) What is the output? int[] arr = new int[3]; print(arr[0]); (In a language like Java where arrays are zero-initialized)

10Multiple Choice

(Comparison) What is the Time Complexity of looking up a value by its Key in a HashMap (on average)?

11Multiple Choice

(Scenario-Based) You are managing a printer queue where the first document sent must be the first one printed. Which data structure is best?

12Multiple Choice

(Conceptual) What happens when a Hash Collision occurs in a HashMap?

13Multiple Choice

(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();

14Multiple Choice

(Calculation) What is the Time Complexity of inserting a new element at the **index 0** of an ArrayList of size N?

15Multiple Choice

(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?

16Multiple Choice

(Conceptual) What is the difference between a Capacity and Size in a dynamic array like ArrayList?

17Multiple Choice

(Debugging) Why is using an Array usually safer for performance-critical code than an ArrayList of Objects?

18Multiple Choice

(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?

19Multiple Choice

(Code-Tracing) Trace the output: set = new HashSet(); set.add("A"); set.add("B"); set.add("A"); print(set.size());

20Multiple Choice

(Conceptual) What is the "Load Factor" of a HashMap?

1Multiple Choice

(Conceptual) Which OOP principle is primarily concerned with reducing "Tight Coupling" between different parts of a system?

2Multiple Choice

(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:

3Multiple Choice

(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();

4Multiple Choice

(Conceptual) What is the "Diamond Problem" in Object-Oriented Programming?

5Multiple Choice

(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?

6Multiple Choice

(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);

7Multiple Choice

(Comparison) What is a key difference between an Interface and an Abstract Class in modern Java (post-Java 8)?

8Multiple Choice

(Conceptual) Which of the following best describes Composition over Inheritance?

9Multiple Choice

(Debugging) Why will the following code fail to compile? class Test { int x = 10; static void display() { print(x); } }

10Multiple Choice

(Conceptual) What is the purpose of a Constructor?

11Multiple Choice

(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?

12Multiple Choice

(Code-Tracing) What is Method Overloading?

13Multiple Choice

(Conceptual) What is the meaning of the final keyword (or sealed in C#) when applied to a **Class**?

14Multiple Choice

(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?

27Multiple Choice

In object-oriented programming, which principle allows a derived class method to replace the implementation of a base class virtual method at runtime?