Chapter 4.3: Introduction to Programming

Introduction to Programming · Study Questions
Programming languageCompiling and interpretingKinds of errorsLanguages and levelsProblem solvingSoftware processBasic constructs
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · Basic Concepts

Question 1: Syntax and Semantics

Which statement about the syntax and semantics of a programming language is true?

B: Syntax defines the form (how it is written), semantics defines the behavior (what it does). A is wrong: a program written according to the rules can still do the wrong thing (a logic error). The same problem can be solved in different languages; the choice depends on the requirements.

Multiple Choice · Basic Concepts

Question 2: Source Code and Machine Language

Which statement about source code and machine language is wrong?

Source codehumans read itTranslation toolcompiler/interpreterMachine language0101 instructionsProcessorexecutes

C is wrong: the processor understands only its own machine language. Source code is first made executable through a compiler or interpreter (in Java, javac + the JVM); a programming language and the processor's language are not the same thing.

Multiple Choice · Execution

Question 3: Compiled, Interpreted, Hybrid

How are the languages C, Python and Java (by their common implementations) classified, in that order?

D: C is compiled ahead of time into the target machine code. Python instructions are executed at run time (via bytecode) and Python is called interpreted. Java source code is compiled to bytecode, and the bytecode is executed on the JVM: hybrid. C# is hybrid too.

Multiple Choice · Execution

Question 4: Three Languages, Three Translation Paths

Which of the following is true about the typical translation paths of languages?

A: A C compiler produces processor-specific machine code. The JVM translates frequently run parts into machine code with JIT (B is wrong); CPython first compiles to bytecode, then interprets it on a virtual machine (C is wrong). Most systems use both approaches together (D is wrong).

Output Question · Execution

Question 5: A Syntax Error: Compiler and Interpreter

Line 3 contains a typing (syntax) error. If the program is run (a) with a compiler, (b) with a simple interpreter that runs line by line, what appears on the screen?

print "Hello"
x ← 10
print (x + 2     // ) is missing!
print "End"
Show answer

(a) Compiler: nothing appears on the screen; only the message "line 3: syntax error" is shown. Because the translation failed, no executable file is produced and the program never starts.

Translation 1–4line 3: errorExecutionnever starts

(b) Simple interpreter: Hello is printed, x = 10 is assigned, and on reaching line 3 it reports the error and stops; "End" is not printed.

Line 1MerhabaLine 2x = 10Line 3error!

Modern Python/JavaScript first parses the whole file and reports the syntax error right at the start.

Step-by-Step Trace · Execution

Question 6: Division by Zero in a Compiled Program

This program is compiled and run. What happens, in order, in the translation and execution phases, and what appears on the screen?

The compiler reads the whole program and checks the syntax. No line has run yet.

x / 0 follows the syntax rules; the translation is successful. Division by 0 is not a syntax error.

Execution starts: line 1 → Hello.

x10

10 is written into the box x in memory.

10 / 0 cannot be computed: a runtime error, the program stops. The compiler could not catch this error.

Line 4 never runs; "End" is not printed. The earlier lines had already run.

print "Hello"
x ← 10
print x / 0
print "End"

Multiple Choice · Kinds of Errors

Question 7: The Wrong Divisor

When computing the average of three grades, the sum was mistakenly divided by 4 instead of 3. The program runs without problems but gives a wrong result. What kind of error is this?

B: The program compiles and runs and there is no error message, but the result is wrong → a logic error. Such errors are found only by testing (comparing the expected and the actual result).

Multiple Choice · Kinds of Errors

Question 8: An Error Caught Before Running

Which of the following errors is caught during compilation, before the program ever runs?

A: A missing semicolon is a syntax error; the compiler reports it with a line number and no program is produced. B occurs at run time (when the input is 0), while C and D are logic errors noticed only through wrong results.

Output Question · Kinds of Errors

Question 9: Why Is the Average 185?

For a student whose grades are 70, 85 and 90, what does this pseudocode print? What is the expected result, and where is the bug?

g1 ← 70
g2 ← 85
g3 ← 90
avg ← g1 + g2 + g3 / 3
print "Average: ", avg
Show answer

Output: Average: 185 (expected 81.67)

  • Division is done before addition: only g3 / 3 = 30 is computed.
  • 70 + 85 + 30 = 185.
  • The program runs and gives no error message: a logic error.
  • Fix: avg ← (g1 + g2 + g3) / 3 → 245 / 3 ≈ 81.67.
g170g285g390avg185

Multiple Choice · Languages

Question 10: Language Level

Which statement about the level of programming languages is true?

B: Assembly is the symbolic form of machine instructions; it is very close to the hardware. Level is not a measure of quality but a level of abstraction: high-level languages (Python, Java) let you write the problem close to the way people express it.

Multiple Choice · Languages

Question 11: Paradigms

The sum of the array grades = {70, 90, 80} is computed in four ways. Which one is an example of the functional approach?

C: The functional approach combines transformations without changing the data; stream().sum() keeps no intermediate state variable. A and D are the imperative approach that changes state step by step; B is the object-oriented approach that combines data and behavior in an object. Java supports all three.

Step-by-Step Trace · Problem Solving

Question 12: Area and Perimeter Pseudocode

With the input width = 3, height = 4, how does the pseudocode run line by line, and what does it print?

width3

width = 3 is read and written into the box "width" in memory.

width3height4

height = 4 is read.

Both conditions are true → the "then" branch is entered.

area = 3 × 4 = 12.

width3height4area12perimeter14

Parentheses first: 3 + 4 = 7, then 2 × 7 = 14.

The text and the value are printed side by side.

The perimeter is printed.

The "else" branch is skipped in this run. The program ends.

read width
read height
if width > 0 and height > 0 then
    area ← width × height
    perimeter ← 2 × (width + height)
    print "Area: ", area
    print "Perimeter: ", perimeter
else
    print "Invalid size"

Output Question · Problem Solving

Question 13: Edge-Case Inputs

The same pseudocode is run twice: (a) width = 0, height = 5 · (b) width = 5, height = 2. What is the output of each run?

read width
read height
if width > 0 and height > 0 then
    area ← width × height
    perimeter ← 2 × (width + height)
    print "Area: ", area
    print "Perimeter: ", perimeter
else
    print "Invalid size"
Show answer

(a) Invalid size

  • 0 > 0 is false; the whole condition joined by "and" becomes false → the else branch. The area and perimeter are never computed.

(b)

Area: 10
Perimeter: 14
  • 5 × 2 = 10 and 2 × (5 + 2) = 14. Different rectangles can have the same perimeter.

0 is an edge case: one of the special inputs that should be tried by hand when designing an algorithm.

Output Question · Problem Solving

Question 14: Grade Average Pseudocode

The pseudocode is run for two students: (a) 70, 85, 90 · (b) 40, 55, 62. What are the outputs? (The average is printed rounded to 2 decimal places.)

read g1, g2, g3
avg ← (g1 + g2 + g3) / 3
if avg ≥ 60 then
    print "Passed: ", avg
else
    print "Failed: ", avg
Show answer

(a) Passed: 81.67 · (b) Failed: 52.33

  • (a) Parentheses first: 70 + 85 + 90 = 245; 245 / 3 ≈ 81.67 ≥ 60 → the "then" branch.
  • (b) 40 + 55 + 62 = 157; 157 / 3 ≈ 52.33 < 60 → the "else" branch.
  • In each run only one of the two branches executes.
g140g255g362avg52.33

Multiple Choice · Software Process

Question 15: Order of the Development Phases

In which option are the phases of the software development process given in the correct order?

RequirementsDesignCodingTestingDeploymentMaintenance

C: First what the user wants (requirements), then the plan (design; the flowchart belongs here), coding, testing (if there are bugs, go back to coding), deployment and maintenance. New requests that arrive during maintenance restart the cycle.

Multiple Choice · Software Process

Question 16: A Vague Requirement

The customer cannot fully describe what they want; they say, "I'll decide when I see the screens." What is the most appropriate first step in this case?

D: Prototyping makes a vague requirement visible; a prototype is not the product itself. In the waterfall model the phases are clear, but going back is costly; the agile approach also gets feedback in short cycles.

Multiple Choice · Basic Constructs

Question 17: Suitable Data Types

In which data types should a student's name, grade average and passed the course? be stored, in that order?

A: A name consists of characters (string; String in Java), an average can be fractional like 81.67 (decimal; double), and the question of passing takes a true/false value (boolean). A data type determines how a value is represented and which operations can be performed.

Step-by-Step Trace · Basic Constructs

Question 18: A Counting-Down Loop

Which numbers does this pseudocode print on the screen? How many times does the loop run?

counter = 10.

10 > 0 → counter = 7 → printed.

7 > 0 → 4.

4 > 0 → 1.

1 > 0 is still true → counter = −2 and −2 is printed too: the condition is checked at the start of the round, not before printing.

−2 > 0 is false → the loop ends. 4 rounds in total.

The line after the loop runs.

counter ← 10
while counter > 0 repeat
    counter ← counter − 3
    print counter
print "done"

Output Question · Basic Constructs

Question 19: Array and Index

Indices start from 0 (as in Java). What is the output of the pseudocode?

arr ← [4, 8, 15, 16, 23]
print arr[2]
print arr[0] + arr[4]
print length(arr)
4081152163234
Show answer
15
27
5
  • arr[2]: since the first item is at index 0, this is the 3rd item → 15 (not 8).
  • arr[0] + arr[4] = 4 + 23 = 27: the first and the last item.
  • There are 5 items; the last index is length − 1 = 4. Asking for arr[5] would go outside the array.

Step-by-Step Trace · Basic Constructs

Question 20: A Function Call

The function is called twice. What is x in each call, what is returned, and what is printed on the screen?

The program skips the function definition and starts at line 4: square(3) is called.

A new call is opened; the input is x = 3. 3 × 3 = 9 is returned.

The call ends and the result is assigned to a: a = 9. The x of square no longer exists.

The same function with a different input: x = 4 → 16 is returned.

a9b16

b = 16.

9 + 16 = 25. A function names a task; it can be used again and again.

function square(x)
    return x × x
end
a ← square(3)
b ← square(4)
print a + b

The End

Introduction to Programming: Study Questions
Lecture slides: Chapter 4.3: Introduction to Programming · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.