Programming Fundamentals

Programming Fundamentals

Introduction

Learn to express solutions as algorithms and implement them using variables, control flow, functions and basic data handling.

Topic-Wise Overview

Problem Solving and Programming

Algorithms, basic computer architecture, compilers and linkers

Data and Expressions

Data types, variables, input/output and arithmetic, comparison and logical operators

Control Structures

Conditional statements, loops and program execution flow

Collections and Functions

Lists, multidimensional lists, modular programming and function calls

Memory and Files

Strings, pointers or references, static and dynamic memory, file input/output

Study Guidance

Start with the core definitions, follow practical examples and check your understanding with independent exercises. The first practice set below introduces core concepts. Additional topic-wise questions and examples will follow.

Practice MCQs — Foundation Set

30 carefully selected foundation questions, with four options and expandable explanations. Pseudocode conventions: ← means assignment; arrays use zero-based indexing; FOR ranges are inclusive where stated; DIV and MOD use nonnegative integers.

Problem Solving and Programming

Q1. What is an algorithm?

  • A. A finite, well-defined sequence of steps to solve a problem
  • B. A computer’s physical memory
  • C. A type of network cable
  • D. A programming-language logo
Show correct answer and explanation

Correct answer: A. A finite, well-defined sequence of steps to solve a problem

An algorithm describes a clear procedure that terminates after finitely many steps.

Q2. Which property means that an algorithm eventually stops?

  • A. Ambiguity
  • B. Finiteness
  • C. Recursion
  • D. Portability
Show correct answer and explanation

Correct answer: B. Finiteness

Finiteness requires an algorithm to terminate after a limited number of steps.

Q3. Which sequence best describes a common problem-solving process?

  • A. Code → guess → test nothing
  • B. Understand → design → implement → test
  • C. Test → delete → guess → publish
  • D. Compile → skip design → stop
Show correct answer and explanation

Correct answer: B. Understand → design → implement → test

A sound workflow begins by understanding requirements, designing an algorithm, implementing it and testing.

Q4. What is the primary function of a compiler?

  • A. Convert source code into a target representation such as machine code
  • B. Store every file permanently
  • C. Control the computer monitor
  • D. Draw a flowchart automatically
Show correct answer and explanation

Correct answer: A. Convert source code into a target representation such as machine code

A compiler translates source code into another form, commonly object or machine code.

Q5. What does a linker commonly do?

  • A. Combine object files and resolve external references
  • B. Measure monitor brightness
  • C. Choose variable names automatically
  • D. Execute only comments
Show correct answer and explanation

Correct answer: A. Combine object files and resolve external references

Linkers combine separately compiled modules and connect referenced symbols and libraries.

Q6. In the classic von Neumann model, where are instructions and data stored?

  • A. Only in the CPU’s arithmetic unit
  • B. In the same main memory
  • C. On separate keyboards
  • D. Only on output devices
Show correct answer and explanation

Correct answer: B. In the same main memory

The stored-program model places instructions and data in addressable memory.

Data and Expressions

Q7. Which of these is a Boolean value?

  • A. -3.5
  • B. “hello”
  • C. TRUE
  • D. 42
Show correct answer and explanation

Correct answer: C. TRUE

A Boolean value is either true or false.

Q8. If x ← 7 and y ← 3, what is x + 2 × y?

  • A. 27
  • B. 13
  • C. 21
  • D. 17
Show correct answer and explanation

Correct answer: B. 13

Multiplication comes first: 2 × 3 = 6, so 7 + 6 = 13.

Q9. With integer division defined as discarding the remainder for nonnegative integers, what is 17 DIV 5?

  • A. 2
  • B. 3
  • C. 3.4
  • D. 4
Show correct answer and explanation

Correct answer: B. 3

17 = 5 × 3 + 2, so the integer quotient is 3.

Q10. What is 17 MOD 5?

  • A. 2
  • B. 3
  • C. 5
  • D. 12
Show correct answer and explanation

Correct answer: A. 2

MOD returns the remainder; dividing 17 by 5 leaves remainder 2.

Q11. If a ← 4, then a ← a + 6, what is the final value of a?

  • A. 4
  • B. 6
  • C. 10
  • D. 24
Show correct answer and explanation

Correct answer: C. 10

The right-hand expression uses the old value: 4 + 6 = 10, then assigns 10 to a.

Q12. Which expression is TRUE when x = 8?

  • A. x < 5
  • B. x = 3
  • C. x > 6 AND x < 10
  • D. x > 10 OR x < 0
Show correct answer and explanation

Correct answer: C. x > 6 AND x < 10

8 is greater than 6 and less than 10, so both conditions are true.

Control Structures

Q13. What is the purpose of an IF statement?

  • A. Repeat code forever
  • B. Select actions based on a condition
  • C. Declare a fixed array length
  • D. Merge object files
Show correct answer and explanation

Correct answer: B. Select actions based on a condition

An IF statement executes a branch according to a Boolean condition.

Q14. If score = 49, what does IF score ≥ 50 THEN print(“Pass”) ELSE print(“Retry”) output?

  • A. Pass
  • B. Retry
  • C. 49
  • D. Nothing
Show correct answer and explanation

Correct answer: B. Retry

49 does not meet the threshold of 50, so the ELSE branch prints Retry.

Q15. What does a WHILE loop check before each iteration?

  • A. Only the filename
  • B. Its loop condition
  • C. The number of CPU cores
  • D. The program’s author
Show correct answer and explanation

Correct answer: B. Its loop condition

A WHILE loop evaluates its condition before executing each iteration.

Q16. In pseudocode, x ← 1; WHILE x < 5: x ← x + 2. What is x after the loop?

  • A. 3
  • B. 4
  • C. 5
  • D. 7
Show correct answer and explanation

Correct answer: C. 5

Starting at 1, x becomes 3 and then 5. Since 5 < 5 is false, the loop stops.

Q17. How many times is the body executed for FOR i ← 1 TO 4 (inclusive)?

  • A. 3
  • B. 4
  • C. 5
  • D. 0
Show correct answer and explanation

Correct answer: B. 4

The index takes the values 1, 2, 3 and 4, yielding four iterations.

Q18. What condition most directly risks an infinite WHILE loop?

  • A. The condition always becomes false
  • B. The condition remains true and there is no other exit
  • C. A variable has a short name
  • D. The program prints a number
Show correct answer and explanation

Correct answer: B. The condition remains true and there is no other exit

If the condition never becomes false and no exit occurs, the loop cannot terminate.

Collections and Functions

Q19. For zero-based array A = [5, 9, 2, 8], what is A[2]?

  • A. 5
  • B. 9
  • C. 2
  • D. 8
Show correct answer and explanation

Correct answer: C. 2

Zero-based positions are 0, 1, 2 and 3; A[2] is the third value, 2.

Q20. Which is the main benefit of placing repeated logic in a function?

  • A. It always eliminates memory use
  • B. It improves reuse and organisation
  • C. It prevents input
  • D. It makes algorithms mathematically incorrect
Show correct answer and explanation

Correct answer: B. It improves reuse and organisation

Functions package reusable behaviour and help structure programs into manageable parts.

Q21. Suppose square(n) returns n × n. What is square(6)?

  • A. 12
  • B. 18
  • C. 36
  • D. 64
Show correct answer and explanation

Correct answer: C. 36

Substituting n = 6 gives 6 × 6 = 36.

Q22. In a two-dimensional array, which pair identifies an element?

  • A. Filename and extension
  • B. Row and column indices
  • C. Password and username
  • D. Compiler and linker
Show correct answer and explanation

Correct answer: B. Row and column indices

A two-dimensional array is indexed using two positions, commonly row and column.

Q23. If a function returns a value, what does RETURN normally do?

  • A. Exit the function while providing that value to its caller
  • B. Delete all variables in the program
  • C. Start an unrelated process
  • D. Convert a string to a file
Show correct answer and explanation

Correct answer: A. Exit the function while providing that value to its caller

RETURN transfers control back to the caller and supplies the result.

Memory and Files

Q24. What is a string in introductory programming?

  • A. A sequence of characters
  • B. A processor register
  • C. A network router
  • D. An arithmetic operator only
Show correct answer and explanation

Correct answer: A. A sequence of characters

A string represents text as a sequence of characters.

Q25. Which operation typically obtains data from a stored file?

  • A. File read
  • B. Screen brightness adjustment
  • C. Variable renaming
  • D. Code indentation
Show correct answer and explanation

Correct answer: A. File read

Reading a file retrieves its stored bytes or text for processing.

Q26. What is dynamic memory allocation?

  • A. Allocating memory during program execution as needed
  • B. Printing an integer
  • C. Changing the keyboard layout
  • D. Converting HTML into a PDF
Show correct answer and explanation

Correct answer: A. Allocating memory during program execution as needed

Dynamic allocation requests memory while a program is running rather than relying solely on predetermined storage.

Q27. What does a pointer generally store in a language such as C?

  • A. A memory address
  • B. A compressed image only
  • C. The number of compiler warnings
  • D. An entire operating system
Show correct answer and explanation

Correct answer: A. A memory address

A pointer holds an address that can identify a memory location.

Q28. Which operation joins the strings “data” and “base” into “database”?

  • A. Subtraction
  • B. Concatenation
  • C. Integer division
  • D. Modulus
Show correct answer and explanation

Correct answer: B. Concatenation

String concatenation combines two sequences of characters end to end.

Q29. Why is closing a file after use generally important?

  • A. It helps release resources and flush buffered data as appropriate
  • B. It changes all integers to strings
  • C. It guarantees perfect security
  • D. It erases the file automatically
Show correct answer and explanation

Correct answer: A. It helps release resources and flush buffered data as appropriate

Closing releases handles and may ensure pending writes are committed through the program’s I/O buffers.

Q30. A program reads one line from a text file. Which case must robust code handle?

  • A. The end of the file
  • B. The speed of light
  • C. The number of display pixels
  • D. Only the variable’s spelling
Show correct answer and explanation

Correct answer: A. The end of the file

End-of-file is a normal condition; code should detect it instead of assuming lines always remain.

Short Questions with Step-by-Step Answers

1. What is the difference between an algorithm and a program?

An algorithm is a language-independent procedure for solving a problem. A program is an implementation of such procedures in a programming language. Example: “read two numbers, add them, print the sum” is an algorithm; actual source code is a program.

2. Explain a compiler and linker using a simple workflow.

Step 1: Write source files. Step 2: A compiler translates source files into object code or another target form. Step 3: A linker combines object code and required libraries, resolving external symbol references. Step 4: The resulting executable can be loaded and run.

3. Explain the difference between DIV and MOD with an example.

For nonnegative integers, DIV produces the whole-number quotient and MOD produces the remainder. Since 23 = 4 × 5 + 3, 23 DIV 4 = 5 and 23 MOD 4 = 3.

4. Find the final value of total in this pseudocode: total ← 0; FOR i ← 1 TO 4 (inclusive): total ← total + i.

Start at total = 0. Add 1 to get 1; add 2 to get 3; add 3 to get 6; add 4 to get 10. Therefore total = 10.

5. What are input, processing and output? Provide an example.

Input: receive the radius r of a circle. Processing: compute A = π × r × r. Output: display the calculated area. These stages help describe a clear algorithm.

6. How can you avoid an infinite loop?

Step 1: State the loop condition clearly. Step 2: Identify which variable controls it. Step 3: Update the variable on each intended iteration. Step 4: Check the stopping condition and test boundary cases. Example: x ← 0; WHILE x < 3: x ← x + 1 terminates with x = 3.

These questions are a foundation set, not a complete question bank for every topic. More topic-wise practice will be added and reviewed progressively.

Intermediate Practice — Questions 31–54

Pseudocode uses zero-based arrays, inclusive FOR loop endpoints and integer DIV/MOD for nonnegative integers.

Algorithms

Q31. What does binary search require?

  • A. Sorted search data
  • B. Only unique values
  • C. A linked list
  • D. No comparisons
Show correct answer and explanation

Correct answer: A. Sorted search data

The ordering permits half of the current search interval to be discarded.

Q32. Starting n = 1, repeatedly double n while n < 20. What is final n?

  • A. 16
  • B. 20
  • C. 32
  • D. 64
Show correct answer and explanation

Correct answer: C. 32

1, 2, 4, 8, 16, 32; at 32 the condition is false.

Q33. Which flowchart symbol commonly means a decision?

  • A. Rectangle
  • B. Diamond
  • C. Oval
  • D. Parallelogram
Show correct answer and explanation

Correct answer: B. Diamond

A diamond denotes branching on a condition.

Q34. Which test case probes an empty-array boundary?

  • A. Length 10
  • B. Length 0
  • C. Length 100
  • D. Length 2
Show correct answer and explanation

Correct answer: B. Length 0

A zero-length input checks handling of no elements.

Expressions

Q35. For x=5 and y=9, evaluate NOT(x > 7 OR y < 4).

  • A. TRUE
  • B. FALSE
  • C. 5
  • D. 9
Show correct answer and explanation

Correct answer: A. TRUE

Both comparisons are false; NOT(false OR false) is true.

Q36. For nonnegative integer arithmetic, evaluate (10 MOD 4) × (10 DIV 4).

  • A. 2
  • B. 4
  • C. 6
  • D. 8
Show correct answer and explanation

Correct answer: B. 4

The remainder and integer quotient are both 2; product is 4.

Q37. Evaluate 3 + 12 / 4 × 2 using usual precedence and left associativity.

  • A. 7
  • B. 9
  • C. 12
  • D. 15
Show correct answer and explanation

Correct answer: B. 9

12 / 4 = 3; 3 × 2 = 6; 3 + 6 = 9.

Q38. NOT(A AND B) is equivalent to which expression?

  • A. NOT A AND NOT B
  • B. A OR B
  • C. NOT A OR NOT B
  • D. A AND NOT B
Show correct answer and explanation

Correct answer: C. NOT A OR NOT B

De Morgan’s law converts the negated conjunction to a disjunction of negations.

Control Flow

Q39. For i from 2 to 6 inclusive with step 2, sum all i. What is the sum?

  • A. 6
  • B. 9
  • C. 12
  • D. 20
Show correct answer and explanation

Correct answer: C. 12

The visited values are 2, 4, 6; their sum is 12.

Q40. If a WHILE condition is initially false, how many times is its body executed?

  • A. 0
  • B. 1
  • C. 2
  • D. Infinitely
Show correct answer and explanation

Correct answer: A. 0

WHILE checks its condition before entering the body.

Q41. What are the valid zero-based indexes of an array of length n > 0?

  • A. 1 through n
  • B. 0 through n
  • C. 0 through n−1
  • D. Only n
Show correct answer and explanation

Correct answer: C. 0 through n−1

The first index is 0 and the last valid index is n−1.

Q42. x ← 12; WHILE x > 3: x ← x DIV 2. What is final x?

  • A. 1
  • B. 2
  • C. 3
  • D. 6
Show correct answer and explanation

Correct answer: C. 3

12 changes to 6 and then 3; the loop then stops.

Arrays and Strings

Q43. For B=[4,7,1,9,2] with zero-based indexing, what is B[1]+B[4]?

  • A. 6
  • B. 8
  • C. 9
  • D. 11
Show correct answer and explanation

Correct answer: C. 9

7 + 2 = 9.

Q44. How many elements are in a rectangular 3-by-4 array?

  • A. 7
  • B. 9
  • C. 12
  • D. 16
Show correct answer and explanation

Correct answer: C. 12

Multiply rows by columns: 3 × 4 = 12.

Q45. What is the character at index 3 in the string LEVEL (zero-based)?

  • A. L
  • B. E
  • C. V
  • D. Out of range
Show correct answer and explanation

Correct answer: B. E

Positions 0–4 are L, E, V, E, L.

Q46. What is typical indexed-access time for a contiguous array A[k]?

  • A. O(1)
  • B. O(n)
  • C. O(n²)
  • D. O(log n)
Show correct answer and explanation

Correct answer: A. O(1)

The index determines the element address in constant time under the standard array model.

Functions and Recursion

Q47. What is needed for terminating recursive computation on valid inputs?

  • A. A base case and progress toward it
  • B. Only global variables
  • C. Two loops
  • D. A file
Show correct answer and explanation

Correct answer: A. A base case and progress toward it

The recursion must reach a case that does not make another recursive call.

Q48. Given factorial(0)=1 and factorial(n)=n×factorial(n−1), what is factorial(4)?

  • A. 10
  • B. 16
  • C. 24
  • D. 120
Show correct answer and explanation

Correct answer: C. 24

4×3×2×1 = 24.

Q49. A function modifies a pass-by-value integer parameter. What happens to the caller’s variable?

  • A. Always changes
  • B. Remains unchanged
  • C. Becomes 0
  • D. Is deleted
Show correct answer and explanation

Correct answer: B. Remains unchanged

Pass-by-value operates on a separate local value.

Q50. The call stack commonly tracks which information?

  • A. Active calls and return information
  • B. Only images
  • C. Files on disk
  • D. Network cables
Show correct answer and explanation

Correct answer: A. Active calls and return information

Each active invocation commonly has a stack frame storing return and local state information.

Memory and Files

Q51. Unreleased dynamic allocations in a manually managed environment may cause what?

  • A. Memory leak
  • B. Syntax highlighting
  • C. Faster code
  • D. Only arithmetic overflow
Show correct answer and explanation

Correct answer: A. Memory leak

Memory that is never freed can accumulate.

Q52. How should a program handle reading past end-of-file?

  • A. Check for EOF or read failure
  • B. Assume data exists
  • C. Restart the OS
  • D. Delete the file
Show correct answer and explanation

Correct answer: A. Check for EOF or read failure

Read results must be checked before processing.

Q53. Which file mode normally adds new data to the end of existing content?

  • A. Read
  • B. Append
  • C. Write-truncate
  • D. Binary-only
Show correct answer and explanation

Correct answer: B. Append

Append writes at the end rather than replacing earlier data.

Q54. What is a dangling pointer?

  • A. A pointer to an object whose lifetime ended
  • B. A valid pointer to the first array element
  • C. A text string
  • D. A pointer holding the count of warnings
Show correct answer and explanation

Correct answer: A. A pointer to an object whose lifetime ended

References to objects that no longer exist are invalid.

Additional Short Questions with Worked Answers

7. Trace sum ← 0; FOR i ← 1 TO 5: IF i MOD 2 = 1 THEN sum ← sum + i.

Odd values 1, 3, and 5 are added. The successive totals are 1, 4, and 9. Final answer: 9.

8. Explain an algorithm to find the maximum of a nonempty array.

Start with the first element as maximum. Visit each remaining element and replace maximum when a larger value appears. After scanning all n elements, return maximum. Time: O(n).

9. Explain pass-by-value and pass-by-reference.

Pass-by-value passes a value into a separate parameter; changes do not directly overwrite the caller’s variable. Pass-by-reference provides access to the original variable. Exact language mechanisms vary.

10. Why is a base case needed in recursion?

The base case returns without further recursive calls. Each non-base step must move toward it; for example factorial(n) reduces n until n=0.

11. List safe steps for reading a file.

Open the file and check success. Read while checking end-of-file and error conditions. Process valid data. Close the file or use automatic resource management.

Advanced Practice — Questions 55–74

Pseudocode conventions: arrays use zero-based indexing; FOR endpoints are inclusive; DIV and MOD refer to nonnegative integer operations.

Algorithm Analysis

Q55. A loop visits every element of an array of size n exactly once. What is its time complexity?

  • A. O(1)
  • B. O(log n)
  • C. O(n)
  • D. O(n²)
Show correct answer and explanation

Correct answer: C. O(n)

There are n visits with constant work each, so total time is O(n).

Q56. Two nested loops each execute n times, with constant work inside. What is their complexity?

  • A. O(n²)
  • B. O(n)
  • C. O(log n)
  • D. O(1)
Show correct answer and explanation

Correct answer: A. O(n²)

There are n × n iterations, so the running time is O(n²).

Q57. Binary search on a sorted array of 64 items requires at most how many comparisons to find an item or establish absence?

  • A. 5
  • B. 6
  • C. 7
  • D. 64
Show correct answer and explanation

Correct answer: C. 7

Successive search intervals have sizes at most 64, 32, 16, 8, 4, 2 and 1, giving at most seven comparisons.

Q58. An algorithm uses two integer variables and no input-sized auxiliary data. What is its auxiliary space complexity?

  • A. O(1)
  • B. O(n)
  • C. O(n²)
  • D. O(2ⁿ)
Show correct answer and explanation

Correct answer: A. O(1)

A constant number of scalar variables needs constant auxiliary space.

Code Tracing

Q59. Given x ← 2; y ← 5; x ← x + y; y ← x − y; x ← x − y, what are (x, y)?

  • A. (2, 5)
  • B. (5, 2)
  • C. (7, 5)
  • D. (5, 7)
Show correct answer and explanation

Correct answer: B. (5, 2)

The assignments give x=7; y=2; x=5. Thus the final pair is (5,2).

Q60. What is x after x ← 0; FOR i ← 1 TO 4 inclusive: x ← x + i × i?

  • A. 10
  • B. 20
  • C. 30
  • D. 40
Show correct answer and explanation

Correct answer: C. 30

The sum of squares is 1 + 4 + 9 + 16 = 30.

Q61. Let a ← 7. IF a MOD 2 = 0 THEN a ← a DIV 2 ELSE a ← 3 × a + 1. What is a?

  • A. 4
  • B. 11
  • C. 21
  • D. 22
Show correct answer and explanation

Correct answer: D. 22

Since 7 is odd, the ELSE branch assigns 3×7+1 = 22.

Q62. For n ← 18; count ← 0; WHILE n > 1: n ← n DIV 2; count ← count + 1, what is count?

  • A. 3
  • B. 4
  • C. 5
  • D. 6
Show correct answer and explanation

Correct answer: B. 4

The values of n after iterations are 9, 4, 2, 1; four iterations occur.

Arrays

Q63. Given A = [3, 1, 4, 1, 5], what is the sum of A[0], A[2] and A[4] with zero-based indexing?

  • A. 8
  • B. 9
  • C. 12
  • D. 13
Show correct answer and explanation

Correct answer: C. 12

The indexed values are 3, 4 and 5; their sum is 12.

Q64. What is the result of reversing the array [2, 7, 9, 4]?

  • A. [4, 9, 7, 2]
  • B. [2, 4, 7, 9]
  • C. [9, 7, 4, 2]
  • D. [4, 7, 9, 2]
Show correct answer and explanation

Correct answer: A. [4, 9, 7, 2]

Reversing swaps the first and last positions and the two middle positions.

Q65. For a 4-row, 3-column array, how many elements are there?

  • A. 7
  • B. 12
  • C. 16
  • D. 24
Show correct answer and explanation

Correct answer: B. 12

The array contains 4 × 3 = 12 elements.

Q66. Which search method can work on an unsorted array without preprocessing?

  • A. Binary search as normally defined
  • B. Linear search
  • C. Interpolation search only
  • D. None
Show correct answer and explanation

Correct answer: B. Linear search

Linear search compares each candidate in turn and does not require sorting.

Functions

Q67. If f(x) returns 2x + 3, what is f(f(1))?

  • A. 5
  • B. 10
  • C. 13
  • D. 16
Show correct answer and explanation

Correct answer: C. 13

First f(1)=5, then f(5)=2×5+3=13.

Q68. Using f(0)=0, f(1)=1, and f(n)=f(n−1)+f(n−2) for n≥2, what is f(6)?

  • A. 5
  • B. 8
  • C. 13
  • D. 21
Show correct answer and explanation

Correct answer: B. 8

The terms from f(0) to f(6) are 0, 1, 1, 2, 3, 5, 8.

Q69. A function with one parameter is called three times sequentially and returns each time. How many completed calls are there?

  • A. 1
  • B. 2
  • C. 3
  • D. 6
Show correct answer and explanation

Correct answer: C. 3

Every invocation that returns is a completed function call; three calls complete.

Q70. What is the main purpose of a function parameter?

  • A. Specify input data for a function
  • B. Force a program to stop
  • C. Allocate a disk partition
  • D. Replace every local variable
Show correct answer and explanation

Correct answer: A. Specify input data for a function

A parameter lets a function receive and use values or references supplied by its caller.

Strings and Files

Q71. In zero-based indexing, what is the index of the last character of a nonempty string of length 9?

  • A. 8
  • B. 9
  • C. 10
  • D. 0
Show correct answer and explanation

Correct answer: A. 8

Indexes start at 0, so the last is length−1 = 8.

Q72. What is the result of concatenating “Hello” with “World” without adding a separator?

  • A. Hello World
  • B. WorldHello
  • C. HelloWorld
  • D. Hello+World
Show correct answer and explanation

Correct answer: C. HelloWorld

Concatenation joins the character sequences directly without an inserted space.

Q73. What may happen if a file is opened for writing in a mode that truncates an existing file?

  • A. Existing contents are discarded
  • B. The file becomes read-only
  • C. The file is always appended
  • D. The file cannot be opened
Show correct answer and explanation

Correct answer: A. Existing contents are discarded

A truncating write mode resets the existing file’s length to zero.

Q74. Which practice helps prevent resource leaks during file processing?

  • A. Leave every file open forever
  • B. Close files or use automatic resource management
  • C. Repeat writes without checking errors
  • D. Avoid testing end-of-file
Show correct answer and explanation

Correct answer: B. Close files or use automatic resource management

Closing files or using scoped resource management releases file handles properly.

Further Short Questions with Worked Answers

12. Trace a nested loop that prints each pair (i, j) for i = 1 to 2 and j = 1 to 3.

The outer loop runs for i = 1, 2. For each i, the inner loop uses j = 1, 2, 3. Six pairs are printed: (1,1), (1,2), (1,3), (2,1), (2,2), (2,3).

13. Describe a linear search and calculate its worst-case time.

Inspect elements sequentially. If the target is at the last position or absent, all n elements may need comparison. The worst-case running time is O(n).

14. Explain the difference between a syntax error and a logic error.

A syntax error breaks a language’s grammar and is typically detected before successful execution. A logic error allows execution but produces an unintended outcome, such as using + instead of ×.

15. Explain why testing boundary values is important.

A program may behave correctly for normal inputs yet fail at limits. Test empty arrays, one-element arrays and maximum allowed values to catch indexing and condition errors.

Practical Programming Exercises — Python

These original practice exercises complement the language-independent course outline. Python is used as an accessible illustration; the same algorithms can be implemented in C, C++ or Java.

Exercise 1: Sum of Even Numbers in a List

Problem: Given a list of integers, calculate the sum of its even elements.

Step-by-step algorithm: Start with zero. Examine every integer. Add a number only when its remainder on division by 2 is zero.

Python Solution

numbers = [3, 8, 5, 12, 7, 4]
total = 0
for number in numbers:
    if number % 2 == 0:
        total += number
print(total)

Expected Output: 24

Explanation: The even elements are 8, 12 and 4. Their sum is 8 + 12 + 4 = 24. The loop visits each list element once.

Exercise 2: Factorial Using a Loop

Problem: Find the factorial of a nonnegative integer n.

Step-by-step algorithm: Set result to 1. Multiply it by each integer from 2 through n. For n = 0, the loop has no iterations and the result stays 1.

Python Solution

n = 5
if n < 0:
    print('Factorial is undefined for negative integers')
else:
    result = 1
    for k in range(2, n + 1):
        result *= k
    print(result)

Expected Output: 120

Explanation: 5! = 1 × 2 × 3 × 4 × 5 = 120. The method also gives 0! = 1.

Exercise 3: Find the Largest Element

Problem: Determine the maximum element of a nonempty list without using max().

Step-by-step algorithm: Assume the first element is largest. Compare each remaining element with it and update the candidate when needed.

Python Solution

values = [7, -2, 15, 9, 11]
if not values:
    print('List is empty')
else:
    largest = values[0]
    for value in values[1:]:
        if value > largest:
            largest = value
    print(largest)

Expected Output: 15

Explanation: Starting at 7, the candidate changes to 15 when 15 is encountered. Subsequent values are smaller. For n elements, the number of comparisons is n − 1.

Exercise 4: Count Vowels in a String

Problem: Count the English vowels in a string, ignoring uppercase versus lowercase.

Step-by-step algorithm: Convert the text to lowercase. Examine each character and increase the counter when it is a, e, i, o or u.

Python Solution

text = 'Education'
count = 0
for character in text.lower():
    if character in 'aeiou':
        count += 1
print(count)

Expected Output: 5

Explanation: Education contains the vowels E, u, a, i and o, so there are five vowels.

Exercise 5: Search for a Number in a List

Problem: Return the zero-based index of the first occurrence of a target, or -1 if it is absent.

Step-by-step algorithm: Start with index -1. Visit list positions in order. On the first match, save the index and stop searching.

Python Solution

values = [6, 2, 9, 2, 5]
target = 2
position = -1
for index, value in enumerate(values):
    if value == target:
        position = index
        break
print(position)

Expected Output: 1

Explanation: The first occurrence of 2 is at zero-based index 1. Linear search takes O(n) time in the worst case.

Exercise 6: Read and Write a Text File

Problem: Write three lines to a file and then count the lines by reading them back.

Step-by-step algorithm: Open the file in write mode and store lines. Close it automatically using with. Reopen it for reading and count the lines.

Python Solution

with open('lesson.txt', 'w', encoding='utf-8') as file:
    file.write('Algorithms\nLoops\nFunctions\n')

with open('lesson.txt', 'r', encoding='utf-8') as file:
    line_count = sum(1 for line in file)

print(line_count)

Expected Output: 3

Explanation: Each newline terminates one written line. The counting expression iterates over the file’s three lines. This program creates or replaces lesson.txt in the current working directory.

Practice tip: Modify the example inputs and test empty, negative and boundary cases where appropriate. The file-handling example writes a local text file.

Further Programming Fundamentals MCQs — Questions 75–150

Q75. What is the purpose of a sentinel value in input processing?

  • A. Allocate more memory
  • B. Sort the input
  • C. Convert a loop into recursion
  • D. Signal a condition such as the end of input
Show correct answer and explanation

Correct answer: D. Signal a condition such as the end of input

A sentinel is a special value used to mark termination or another control condition.

Q76. What is a precondition of a function?

  • A. A condition expected to hold before the function is called
  • B. A result guaranteed after successful execution
  • C. A syntax error
  • D. A type of loop
Show correct answer and explanation

Correct answer: A. A condition expected to hold before the function is called

Preconditions specify assumptions that callers should satisfy.

Q77. Which statement about local variables is generally correct?

  • A. They cannot have values
  • B. Their scope is limited to the block or function where declared
  • C. They are always global
  • D. They automatically persist in files
Show correct answer and explanation

Correct answer: B. Their scope is limited to the block or function where declared

Local variables are accessible only within their declared scope.

Q78. What is a nested loop?

  • A. A function with two return types
  • B. A compiler pass
  • C. A loop contained within another loop
  • D. A loop without a condition
Show correct answer and explanation

Correct answer: C. A loop contained within another loop

A nested loop executes as part of an enclosing loop body.

Q79. Which symbol conventionally denotes assignment in the pseudocode used here?

  • A. ==
  • B. ↔
  • C. ∪
  • D. ←
Show correct answer and explanation

Correct answer: D. ←

The left arrow indicates storing the evaluated right-hand value in a variable.

Q80. What is an off-by-one error?

  • A. A mistake involving one extra or missing iteration or index
  • B. A division by zero in every case
  • C. A type conversion that always succeeds
  • D. A network protocol
Show correct answer and explanation

Correct answer: A. A mistake involving one extra or missing iteration or index

Off-by-one errors often arise from incorrect loop boundaries.

Q81. What is the role of a function’s return type?

  • A. Count the parameters only
  • B. Specify the kind of result the function produces
  • C. Set the operating-system version
  • D. Determine the file extension
Show correct answer and explanation

Correct answer: B. Specify the kind of result the function produces

A return type states the result’s expected data type.

Q82. What is the result of a logical AND when one operand is false?

  • A. Undefined in every language
  • B. A floating-point value
  • C. False
  • D. True
Show correct answer and explanation

Correct answer: C. False

A conjunction is false whenever either operand is false.

Q83. Which structure is most appropriate when choosing among several fixed alternatives?

  • A. An unconditional infinite loop
  • B. A single variable declaration
  • C. A file pointer
  • D. A multiway selection such as switch or if-else-if
Show correct answer and explanation

Correct answer: D. A multiway selection such as switch or if-else-if

Multiway selection expresses several mutually exclusive choices.

Q84. What is a software test case?

  • A. An input and expected outcome used to check a program
  • B. A compiler executable
  • C. A monitor setting
  • D. A variable name only
Show correct answer and explanation

Correct answer: A. An input and expected outcome used to check a program

Test cases compare observed behavior with specified expectations.

Q85. What is debugging?

  • A. Changing program language automatically
  • B. Finding and correcting defects in software
  • C. Deleting all comments
  • D. Measuring screen resolution
Show correct answer and explanation

Correct answer: B. Finding and correcting defects in software

Debugging isolates and fixes incorrect program behavior.

Q86. Why should input be validated?

  • A. To remove the need for testing
  • B. To guarantee faster hardware
  • C. To reject or handle malformed or out-of-range data
  • D. To eliminate all program bugs
Show correct answer and explanation

Correct answer: C. To reject or handle malformed or out-of-range data

Validation checks that supplied data meets program requirements.

Q87. What does a Boolean expression evaluate to?

  • A. A file name in every case
  • B. An array address only
  • C. An operating-system command
  • D. A truth value
Show correct answer and explanation

Correct answer: D. A truth value

Boolean expressions represent true or false conditions.

Q88. What is a loop invariant?

  • A. A property maintained before and after each iteration
  • B. A value that must increase
  • C. A comment used for formatting
  • D. A loop that never ends
Show correct answer and explanation

Correct answer: A. A property maintained before and after each iteration

Invariants help establish the correctness of iterative algorithms.

Q89. Why is modular programming useful?

  • A. It removes all runtime costs
  • B. It separates a program into manageable reusable units
  • C. It prevents the use of parameters
  • D. It requires global state
Show correct answer and explanation

Correct answer: B. It separates a program into manageable reusable units

Modules improve organization, reuse, testing and maintenance.

Q90. What is a parameter in a function definition?

  • A. A machine instruction
  • B. The return address alone
  • C. A named input variable or placeholder
  • D. Always a global file
Show correct answer and explanation

Correct answer: C. A named input variable or placeholder

Parameters receive values or references supplied by function calls.

Q91. What is an argument in a function call?

  • A. A syntax error
  • B. The compiler version
  • C. A loop label only
  • D. A value or expression passed to a parameter
Show correct answer and explanation

Correct answer: D. A value or expression passed to a parameter

Arguments supply the actual data used by the called function.

Q92. What is a base case in recursion?

  • A. A case that returns without further recursive calls
  • B. The longest input
  • C. A case that always recurses
  • D. An unused parameter
Show correct answer and explanation

Correct answer: A. A case that returns without further recursive calls

Base cases stop recursive expansion and allow computation to unwind.

Q93. What is a dry run of an algorithm?

  • A. Disconnecting storage
  • B. Tracing its steps manually with sample input
  • C. Removing its output
  • D. Automatically optimizing machine code
Show correct answer and explanation

Correct answer: B. Tracing its steps manually with sample input

A dry run follows intermediate values to verify program logic.

Q94. Which data type naturally represents a yes-or-no flag?

  • A. File handle
  • B. Floating point exclusively
  • C. Boolean
  • D. Character array only
Show correct answer and explanation

Correct answer: C. Boolean

A Boolean stores one of two truth values.

Q95. What is the difference between syntax and semantic errors?

  • A. They always mean the same thing
  • B. Syntax errors only happen in hardware
  • C. Semantic errors are always fixed by indentation
  • D. Syntax errors violate language grammar; semantic errors concern meaning or behavior
Show correct answer and explanation

Correct answer: D. Syntax errors violate language grammar; semantic errors concern meaning or behavior

Syntax checks grammatical form, while semantics concerns program meaning.

Q96. What is an array index?

  • A. A position identifier used to access an array element
  • B. A processor temperature
  • C. The number of source files
  • D. A sorting algorithm
Show correct answer and explanation

Correct answer: A. A position identifier used to access an array element

Indexing selects an element at a particular position.

Q97. Why is an empty input an important test case?

  • A. It removes the need for assertions
  • B. It can expose boundary-condition defects
  • C. It guarantees successful sorting
  • D. It makes loops run twice
Show correct answer and explanation

Correct answer: B. It can expose boundary-condition defects

Programs must handle an input size of zero when permitted.

Q98. What does an assertion express in a program?

  • A. A way to rename a function
  • B. An instruction to ignore errors
  • C. A condition expected to be true at that point
  • D. A request to delete code
Show correct answer and explanation

Correct answer: C. A condition expected to be true at that point

Assertions document and check expected properties during execution.

Q99. What is short-circuit evaluation of logical AND?

  • A. Both operands must always be evaluated
  • B. It changes AND into OR
  • C. It converts truth values into integers
  • D. The second operand need not be evaluated when the first is false
Show correct answer and explanation

Correct answer: D. The second operand need not be evaluated when the first is false

With short-circuit AND, a false first operand determines the result.

Q100. Which loop is appropriate when the number of repetitions is known beforehand?

  • A. A counted for loop
  • B. Only unbounded recursion
  • C. A file-write statement
  • D. A constructor
Show correct answer and explanation

Correct answer: A. A counted for loop

A for loop naturally expresses a fixed or countable iteration range.

Q101. What is a function’s side effect?

  • A. Its indentation
  • B. An observable change outside its returned value
  • C. Its parameter count
  • D. Its source-code filename
Show correct answer and explanation

Correct answer: B. An observable change outside its returned value

Writing a file or modifying external state is a side effect.

Q102. What is the purpose of a comment in source code?

  • A. Increase CPU frequency
  • B. Automatically test all branches
  • C. Explain intent for people reading the code
  • D. Guarantee runtime correctness
Show correct answer and explanation

Correct answer: C. Explain intent for people reading the code

Comments document logic but are not normally executed.

Q103. What is a constant in programming?

  • A. A loop that repeats indefinitely
  • B. A compiler message
  • C. Any pointer
  • D. A value or binding intended not to change during its defined use
Show correct answer and explanation

Correct answer: D. A value or binding intended not to change during its defined use

Constants represent fixed values or nonmodifiable bindings.

Q104. What does a function call stack frame typically contain?

  • A. Invocation-specific state and return information
  • B. All files on disk
  • C. Only network packets
  • D. Every program’s source code
Show correct answer and explanation

Correct answer: A. Invocation-specific state and return information

Stack frames maintain state for active calls.

Q105. What is the usual purpose of a break statement in a loop?

  • A. Convert all values to strings
  • B. Exit the nearest enclosing loop or switch where applicable
  • C. Restart the operating system
  • D. Declare an array
Show correct answer and explanation

Correct answer: B. Exit the nearest enclosing loop or switch where applicable

Break transfers control beyond the applicable loop or switch.

Q106. What does continue usually do within a loop?

  • A. Compile the current function
  • B. Delete loop variables
  • C. Skip to the next iteration
  • D. Exit the whole program
Show correct answer and explanation

Correct answer: C. Skip to the next iteration

Continue bypasses the remaining current loop body and advances iteration.

Q107. When does a while loop execute zero times?

  • A. Whenever a variable is numeric
  • B. Only if it has no body
  • C. Never
  • D. When its condition is initially false
Show correct answer and explanation

Correct answer: D. When its condition is initially false

The condition is tested before entering a while loop.

Q108. What is the purpose of a function prototype in C or C++?

  • A. Declare a function interface before its use
  • B. Guarantee an algorithm terminates
  • C. Create a new file automatically
  • D. Prevent parameter passing
Show correct answer and explanation

Correct answer: A. Declare a function interface before its use

A prototype informs the compiler about parameter and return types.

Q109. Why should a file-open operation be checked?

  • A. It sets the number of loops
  • B. It can fail due to missing files or permissions
  • C. It always succeeds
  • D. It is needed only for arithmetic
Show correct answer and explanation

Correct answer: B. It can fail due to missing files or permissions

Robust programs handle possible I/O failures.

Q110. What is a memory leak?

  • A. A correct bounds check
  • B. A type of integer literal
  • C. Allocated memory that is no longer released or appropriately reclaimed
  • D. A repeated function name
Show correct answer and explanation

Correct answer: C. Allocated memory that is no longer released or appropriately reclaimed

Unreleased resources can accumulate during execution.

Q111. Evaluate the pseudocode: x←3; y←2; x←2*x+y. What is x?

  • A. 9
  • B. 7
  • C. 10
  • D. 8
Show correct answer and explanation

Correct answer: D. 8

Substitute x=3 and y=2: 2×3+2=8.

Q112. Evaluate the pseudocode: x←4; y←3; x←2*x+y. What is x?

  • A. 11
  • B. 12
  • C. 10
  • D. 13
Show correct answer and explanation

Correct answer: A. 11

Substitute x=4 and y=3: 2×4+3=11.

Q113. Evaluate the pseudocode: x←5; y←4; x←2*x+y. What is x?

  • A. 16
  • B. 14
  • C. 15
  • D. 13
Show correct answer and explanation

Correct answer: B. 14

Substitute x=5 and y=4: 2×5+4=14.

Q114. Evaluate the pseudocode: x←6; y←5; x←2*x+y. What is x?

  • A. 16
  • B. 19
  • C. 17
  • D. 18
Show correct answer and explanation

Correct answer: C. 17

Substitute x=6 and y=5: 2×6+5=17.

Q115. Evaluate the pseudocode: x←7; y←6; x←2*x+y. What is x?

  • A. 21
  • B. 19
  • C. 22
  • D. 20
Show correct answer and explanation

Correct answer: D. 20

Substitute x=7 and y=6: 2×7+6=20.

Q116. Evaluate the pseudocode: x←8; y←7; x←2*x+y. What is x?

  • A. 23
  • B. 24
  • C. 22
  • D. 25
Show correct answer and explanation

Correct answer: A. 23

Substitute x=8 and y=7: 2×8+7=23.

Q117. Evaluate the pseudocode: x←9; y←8; x←2*x+y. What is x?

  • A. 28
  • B. 26
  • C. 27
  • D. 25
Show correct answer and explanation

Correct answer: B. 26

Substitute x=9 and y=8: 2×9+8=26.

Q118. Evaluate the pseudocode: x←10; y←9; x←2*x+y. What is x?

  • A. 28
  • B. 31
  • C. 29
  • D. 30
Show correct answer and explanation

Correct answer: C. 29

Substitute x=10 and y=9: 2×10+9=29.

Q119. Evaluate the pseudocode: x←11; y←10; x←2*x+y. What is x?

  • A. 33
  • B. 31
  • C. 34
  • D. 32
Show correct answer and explanation

Correct answer: D. 32

Substitute x=11 and y=10: 2×11+10=32.

Q120. Evaluate the pseudocode: x←12; y←11; x←2*x+y. What is x?

  • A. 35
  • B. 36
  • C. 34
  • D. 37
Show correct answer and explanation

Correct answer: A. 35

Substitute x=12 and y=11: 2×12+11=35.

Q121. In an inclusive loop FOR i←1 TO 4, add i to total initially zero. Find total.

  • A. 4
  • B. 10
  • C. 11
  • D. 9
Show correct answer and explanation

Correct answer: B. 10

The total is the arithmetic sum 4(4+1)/2=10.

Q122. In an inclusive loop FOR i←1 TO 5, add i to total initially zero. Find total.

  • A. 14
  • B. 5
  • C. 15
  • D. 16
Show correct answer and explanation

Correct answer: C. 15

The total is the arithmetic sum 5(5+1)/2=15.

Q123. In an inclusive loop FOR i←1 TO 6, add i to total initially zero. Find total.

  • A. 22
  • B. 20
  • C. 6
  • D. 21
Show correct answer and explanation

Correct answer: D. 21

The total is the arithmetic sum 6(6+1)/2=21.

Q124. In an inclusive loop FOR i←1 TO 7, add i to total initially zero. Find total.

  • A. 28
  • B. 29
  • C. 27
  • D. 7
Show correct answer and explanation

Correct answer: A. 28

The total is the arithmetic sum 7(7+1)/2=28.

Q125. In an inclusive loop FOR i←1 TO 8, add i to total initially zero. Find total.

  • A. 8
  • B. 36
  • C. 37
  • D. 35
Show correct answer and explanation

Correct answer: B. 36

The total is the arithmetic sum 8(8+1)/2=36.

Q126. In an inclusive loop FOR i←1 TO 9, add i to total initially zero. Find total.

  • A. 44
  • B. 9
  • C. 45
  • D. 46
Show correct answer and explanation

Correct answer: C. 45

The total is the arithmetic sum 9(9+1)/2=45.

Q127. In an inclusive loop FOR i←1 TO 10, add i to total initially zero. Find total.

  • A. 56
  • B. 54
  • C. 10
  • D. 55
Show correct answer and explanation

Correct answer: D. 55

The total is the arithmetic sum 10(10+1)/2=55.

Q128. In an inclusive loop FOR i←1 TO 11, add i to total initially zero. Find total.

  • A. 66
  • B. 67
  • C. 65
  • D. 11
Show correct answer and explanation

Correct answer: A. 66

The total is the arithmetic sum 11(11+1)/2=66.

Q129. In an inclusive loop FOR i←1 TO 12, add i to total initially zero. Find total.

  • A. 12
  • B. 78
  • C. 79
  • D. 77
Show correct answer and explanation

Correct answer: B. 78

The total is the arithmetic sum 12(12+1)/2=78.

Q130. In an inclusive loop FOR i←1 TO 13, add i to total initially zero. Find total.

  • A. 90
  • B. 13
  • C. 91
  • D. 92
Show correct answer and explanation

Correct answer: C. 91

The total is the arithmetic sum 13(13+1)/2=91.

Q131. For nonnegative integer n=6, compute n MOD 4.

  • A. 3
  • B. 0
  • C. 1
  • D. 2
Show correct answer and explanation

Correct answer: D. 2

Modulo gives the remainder when 6 is divided by 4, which is 2.

Q132. For nonnegative integer n=7, compute n MOD 4.

  • A. 3
  • B. 0
  • C. 1
  • D. 2
Show correct answer and explanation

Correct answer: A. 3

Modulo gives the remainder when 7 is divided by 4, which is 3.

Q133. For nonnegative integer n=8, compute n MOD 4.

  • A. 3
  • B. 0
  • C. 1
  • D. 2
Show correct answer and explanation

Correct answer: B. 0

Modulo gives the remainder when 8 is divided by 4, which is 0.

Q134. For nonnegative integer n=9, compute n MOD 4.

  • A. 3
  • B. 0
  • C. 1
  • D. 2
Show correct answer and explanation

Correct answer: C. 1

Modulo gives the remainder when 9 is divided by 4, which is 1.

Q135. For nonnegative integer n=10, compute n MOD 4.

  • A. 3
  • B. 0
  • C. 1
  • D. 2
Show correct answer and explanation

Correct answer: D. 2

Modulo gives the remainder when 10 is divided by 4, which is 2.

Q136. For nonnegative integer n=11, compute n MOD 4.

  • A. 3
  • B. 0
  • C. 1
  • D. 2
Show correct answer and explanation

Correct answer: A. 3

Modulo gives the remainder when 11 is divided by 4, which is 3.

Q137. For nonnegative integer n=12, compute n MOD 4.

  • A. 3
  • B. 0
  • C. 1
  • D. 2
Show correct answer and explanation

Correct answer: B. 0

Modulo gives the remainder when 12 is divided by 4, which is 0.

Q138. For nonnegative integer n=13, compute n MOD 4.

  • A. 3
  • B. 0
  • C. 1
  • D. 2
Show correct answer and explanation

Correct answer: C. 1

Modulo gives the remainder when 13 is divided by 4, which is 1.

Q139. For nonnegative integer n=14, compute n MOD 4.

  • A. 3
  • B. 0
  • C. 1
  • D. 2
Show correct answer and explanation

Correct answer: D. 2

Modulo gives the remainder when 14 is divided by 4, which is 2.

Q140. For nonnegative integer n=15, compute n MOD 4.

  • A. 3
  • B. 0
  • C. 1
  • D. 2
Show correct answer and explanation

Correct answer: A. 3

Modulo gives the remainder when 15 is divided by 4, which is 3.

Q141. Suppose square(x) returns x*x. Evaluate square(3).

  • A. 6
  • B. 9
  • C. 10
  • D. 8
Show correct answer and explanation

Correct answer: B. 9

The function multiplies 3 by itself, giving 9.

Q142. Suppose square(x) returns x*x. Evaluate square(4).

  • A. 15
  • B. 8
  • C. 16
  • D. 17
Show correct answer and explanation

Correct answer: C. 16

The function multiplies 4 by itself, giving 16.

Q143. Suppose square(x) returns x*x. Evaluate square(5).

  • A. 26
  • B. 24
  • C. 10
  • D. 25
Show correct answer and explanation

Correct answer: D. 25

The function multiplies 5 by itself, giving 25.

Q144. Suppose square(x) returns x*x. Evaluate square(6).

  • A. 36
  • B. 37
  • C. 35
  • D. 12
Show correct answer and explanation

Correct answer: A. 36

The function multiplies 6 by itself, giving 36.

Q145. Suppose square(x) returns x*x. Evaluate square(7).

  • A. 14
  • B. 49
  • C. 50
  • D. 48
Show correct answer and explanation

Correct answer: B. 49

The function multiplies 7 by itself, giving 49.

Q146. Suppose square(x) returns x*x. Evaluate square(8).

  • A. 63
  • B. 16
  • C. 64
  • D. 65
Show correct answer and explanation

Correct answer: C. 64

The function multiplies 8 by itself, giving 64.

Q147. Suppose square(x) returns x*x. Evaluate square(9).

  • A. 82
  • B. 80
  • C. 18
  • D. 81
Show correct answer and explanation

Correct answer: D. 81

The function multiplies 9 by itself, giving 81.

Q148. Suppose square(x) returns x*x. Evaluate square(10).

  • A. 100
  • B. 101
  • C. 99
  • D. 20
Show correct answer and explanation

Correct answer: A. 100

The function multiplies 10 by itself, giving 100.

Q149. Suppose square(x) returns x*x. Evaluate square(11).

  • A. 22
  • B. 121
  • C. 122
  • D. 120
Show correct answer and explanation

Correct answer: B. 121

The function multiplies 11 by itself, giving 121.

Q150. Suppose square(x) returns x*x. Evaluate square(12).

  • A. 143
  • B. 24
  • C. 144
  • D. 145
Show correct answer and explanation

Correct answer: C. 144

The function multiplies 12 by itself, giving 144.

Interactive MCQ Practice

Practise one MCQ at a time. Reveal the correct answer and explanation before proceeding to the next question.

Start Programming Fundamentals — One Question at a Time

Explore Other Subjects

Browse Computer Science subjects