Programming Concepts: Languages, Translators, OOP and Code Tracing
A program is a set of instructions that tells a computer how to solve a problem, written in a programming language. Computer papers for lecturer, IT assistant and programmer posts, and the computer portion of ICS and ECAT, ask about generations of languages, translators, types of errors, loops, object-oriented terms and flowchart symbols, and increasingly include short pieces of code to trace by hand. This lesson uses plain pseudocode, where β means 'store the value on the right in the variable on the left'.
Asked in: PPSC, FPSC, NTS, ETEA, KPPSC, SSC, IBPS and ICS/ECAT computer
Generations of programming languages
First-generation language (1GL) is machine language: instructions written directly in binary 0s and 1s. The CPU understands it without translation, so it runs fastest, but it is very hard to write and is machine dependent (a program for one processor will not run on another).
Second-generation language (2GL) is assembly language, which replaces binary codes with short memory aids called mnemonics, such as ADD, SUB, MOV and JMP. It is still machine dependent and must be converted by an assembler. Machine and assembly languages together are called low-level languages.
Third-generation languages (3GL) are high-level, procedural languages with English-like statements, such as FORTRAN, COBOL, BASIC, Pascal, C, C++, Java and Python. They are machine independent (portable) and need a compiler or interpreter. Fourth-generation languages (4GL) are closer to human language and are largely non-procedural: you say what you want rather than how to get it; SQL and report generators are standard examples. Fifth-generation languages (5GL) are used in artificial intelligence and solve problems from constraints and logic, Prolog being the usual example.
- 1GL: machine language (binary), no translation needed
- 2GL: assembly language (mnemonics), needs an assembler
- 3GL: high-level procedural (C, Java, Python, FORTRAN, COBOL)
- 4GL: non-procedural, e.g. SQL
- 5GL: logic/AI, e.g. Prolog
- Low-level = 1GL + 2GL; machine dependent
Translators and types of errors
The program a programmer writes is source code; its machine-language translation is object code. An assembler converts assembly language into machine code. A compiler translates an entire high-level program at once, lists all errors after translation and produces an executable file that can run again without the compiler; C and C++ are normally compiled. An interpreter translates and executes one statement at a time, stops at the first error and is needed every time the program runs; it is convenient for testing. A linker joins object files and library code into one executable, and a loader places the executable in memory to run.
A syntax error breaks the grammar rules of the language, such as a misspelt keyword or a missing bracket; it is caught by the compiler or interpreter before the program runs. A runtime error occurs while the program is running, such as dividing by zero or opening a file that does not exist, and usually stops the program. A logical error lets the program run to the end but gives the wrong answer, for example adding instead of averaging; the translator cannot detect it.
A bug is any error in a program, and debugging is the process of finding and removing bugs. Comments are notes for human readers that the translator ignores.
- Source code β translator β object code
- Compiler: whole program at once, makes an executable
- Interpreter: one statement at a time, needed on each run
- Linker: joins object files; Loader: loads into memory
- Syntax error: grammar; Runtime error: during execution; Logical error: wrong result
Variables, constants, data types and operators
A variable is a named memory location whose value can change while the program runs; a constant holds a value that does not change, such as PI. The name given to a variable, function or class is an identifier. In most languages an identifier must start with a letter or underscore, may contain letters, digits and underscores, cannot contain spaces and cannot be a reserved keyword such as if, for or while.
The data type tells the computer what kind of value is stored and how much memory to use. Common types are integer (whole numbers such as 25), float or real (numbers with a decimal point such as 3.75), character (a single symbol such as 'A'), string (text such as "Lahore" in quotes) and Boolean (only true or false).
Arithmetic operators are +, β, Γ (written as a star in most real languages), / for division, MOD for the remainder and DIV for whole-number division: 17 MOD 5 is 2 and 17 DIV 5 is 3. Relational operators compare values (=, β , less than, greater than, β€, β₯) and give a Boolean result. Logical operators AND, OR and NOT combine conditions. Assignment stores a value: total β total + 5 adds 5 to the old value of total.
- Variable: value can change; Constant: fixed value
- Identifier: starts with letter or _, no spaces, not a keyword
- Types: integer, float/real, character, string, Boolean
- 17 MOD 5 = 2 (remainder); 17 DIV 5 = 3 (whole quotient)
- Relational operators give true or false
Control structures
Structured programs are built from only three control structures. Sequence runs statements one after another in order. Selection chooses between paths using IF, IF-ELSE, nested IF or a CASE (switch) statement. Iteration (repetition, looping) repeats a block of statements.
A FOR loop is used when the number of repetitions is known: FOR i β 1 TO 5 runs the body five times. A WHILE loop tests its condition before each pass (a pre-test or entry-controlled loop), so if the condition is false at the start the body runs zero times. A DO-WHILE or REPEAT-UNTIL loop tests its condition after each pass (a post-test or exit-controlled loop), so the body always runs at least once.
A loop whose condition never becomes false is an infinite loop. A loop inside another loop is a nested loop: if the outer loop runs 3 times and the inner loop runs 4 times for each outer pass, the inner body runs 3 Γ 4 = 12 times. A function (procedure, subroutine) is a named block of code that can be called many times; values passed to it are arguments or parameters.
- Three structures: sequence, selection, iteration
- FOR: known count; WHILE: pre-test, may run 0 times
- DO-WHILE / REPEAT-UNTIL: post-test, runs at least once
- Nested loops: total passes = outer count Γ inner count
Algorithms, pseudocode and flowcharts
An algorithm is a finite, step-by-step procedure for solving a problem. The word comes from the name of the 9th-century Persian mathematician Muhammad ibn Musa al-Khwarizmi. A good algorithm has finiteness (it ends after a limited number of steps), definiteness (each step is clear and unambiguous), zero or more inputs, at least one output, and effectiveness (each step is simple enough to be carried out).
Pseudocode writes an algorithm in structured, English-like statements without the strict syntax of a real language. A flowchart shows an algorithm as a diagram of standard symbols joined by arrows.
Flowchart symbols: the oval (terminal) marks Start and End; the parallelogram shows input or output, such as READ marks or PRINT result; the rectangle shows a process or calculation; the diamond shows a decision with Yes and No branches; arrows (flow lines) show the direction of flow; a small circle is a connector that joins parts of a chart; and a rectangle with double vertical side lines is a predefined process (a subroutine defined elsewhere).
- Algorithm: finite, definite, input, output, effective
- Oval: start/end; Parallelogram: input/output
- Rectangle: process; Diamond: decision
- Arrow: flow line; Small circle: connector
- Double-sided rectangle: predefined process (subroutine)
Object-oriented programming (OOP)
Procedural programming (as in C) organises a program as a sequence of functions acting on data. Object-oriented programming organises it around objects that hold both data and the functions that work on that data. C++, Java, C# and Python support OOP.
A class is a blueprint or template that defines attributes (data members) and methods (member functions). An object is an instance of a class: Car is a class, and a particular car with registration LEA-1234 is an object. A constructor is a special method that runs automatically when an object is created and gives its attributes starting values.
The four pillars of OOP are encapsulation, abstraction, inheritance and polymorphism. Encapsulation bundles data with the methods that use it and hides the data from direct outside access (data hiding), usually by making attributes private. Abstraction shows only the essential features and hides the complex details of how they work. Inheritance lets a child (derived) class acquire the attributes and methods of a parent (base) class, promoting code reuse; Dog inheriting from Animal is the classic example. Polymorphism means one name, many forms: method overloading (same method name with different parameters in one class) is resolved at compile time, and method overriding (a child class redefining a method it inherited) is resolved at run time.
- Class = blueprint; Object = instance of a class
- Constructor: runs automatically when an object is created
- Pillars: Encapsulation, Abstraction, Inheritance, Polymorphism
- Overloading: same name, different parameters (compile time)
- Overriding: child redefines parent's method (run time)
Solved examples
- Trace the pseudocode and give the output: x β 5; y β 3; x β x + y; y β x β y; x β x β y; PRINT x, y
- After x β x + y: x = 8, y = 3.
- After y β x β y: y = 8 β 3 = 5.
- After x β x β y: x = 8 β 5 = 3. The code swaps the two values without a third variable.
Answer: 3 5 - What is printed? sum β 0; FOR i β 1 TO 4: sum β sum + i Γ i; END FOR; PRINT sum
- The loop adds the square of i for i = 1, 2, 3 and 4.
- sum = 0 + 1 = 1, then 1 + 4 = 5, then 5 + 9 = 14, then 14 + 16 = 30.
- The PRINT statement is after the loop, so only the final value is shown.
Answer: 30 - Classify each error: (i) a programmer types PRNT instead of PRINT; (ii) a program to find an average divides by 3 instead of 4 and prints a wrong answer; (iii) a program stops when the user enters 0 as a divisor.
- (i) A misspelt keyword breaks the language rules and is caught during translation: syntax error.
- (ii) The program runs normally but the answer is wrong: logical error.
- (iii) Division by zero happens only while the program is running: runtime error.
Answer: (i) syntax, (ii) logical, (iii) runtime - Sara draws a flowchart to read two numbers, find the larger and print it. Which symbol does she use for each of: Start, READ a, b, the test 'is a β₯ b?', the step big β a, and PRINT big?
- Start and End use the oval (terminal) symbol.
- READ a, b and PRINT big are input and output, so both use the parallelogram.
- The test with Yes and No branches uses the diamond, and the assignment big β a is a process, shown as a rectangle.
Answer: Oval; parallelogram; diamond; rectangle; parallelogram - Compare the two loops. A: count β 10; WHILE count β€ 5: PRINT count; count β count + 1. B: count β 10; REPEAT: PRINT count; count β count + 1; UNTIL count β₯ 5. How many values does each print?
- Loop A tests first. 10 β€ 5 is false, so the body never runs: nothing is printed.
- Loop B runs the body first, printing 10 and making count 11, then tests 11 β₯ 5, which is true, so it stops.
- This is the key difference: a pre-test loop can run zero times, a post-test loop always runs at least once.
Answer: Loop A prints nothing; Loop B prints 10 once - In a school system, class Person has attributes name and CNIC and a method display(). Class Teacher is defined as a child of Person, adds subject, and writes its own display() that also shows the subject. Name the two OOP features used.
- Teacher receives name, CNIC and display() from Person without rewriting them: inheritance.
- Teacher replaces the inherited display() with its own version having the same name and parameters: method overriding.
- Overriding is run-time polymorphism, because the version called depends on whether the object is a Person or a Teacher.
Answer: Inheritance and polymorphism (method overriding)
Exam shortcuts
- Mnemonics such as MOV and ADD mean assembly language; SQL means 4GL; Prolog means 5GL.
- Whole program at once means compiler; line by line means interpreter; assembly to machine code means assembler.
- Wrong grammar is a syntax error, a crash while running is a runtime error, and a wrong answer with no crash is a logical error.
- When tracing, write a small table with one column per variable and update it after every line.
- Flowchart shapes: oval ends, parallelogram talks (input/output), rectangle works (process), diamond decides.
Common mistakes
- Saying an interpreter produces an executable file. Only a compiler produces a stand-alone executable; an interpreter is needed every time.
- Assuming a WHILE loop runs at least once. It checks the condition first and may not run at all; only DO-WHILE or REPEAT-UNTIL guarantees one pass.
- Mixing up overloading and overriding. Overloading is the same name with different parameters in one class; overriding is a child class replacing an inherited method.
- Using a rectangle for a decision in a flowchart. Decisions use the diamond; rectangles are for processing steps.
Practice MCQs
- A language that uses mnemonics such as ADD, MOV and JMP instead of binary codes is:
A) Assembly language
B) Machine language
C) A high-level language
D) A fourth-generation language
Answer: A) Assembly language β Assembly language (2GL) replaces binary instructions with short mnemonics and is converted by an assembler. Machine language is pure binary, while high-level and fourth-generation languages use English-like statements rather than processor mnemonics. - Which translator converts an entire high-level program into machine code before it runs and produces an executable file?
A) Interpreter
B) Assembler
C) Compiler
D) Loader
Answer: C) Compiler β A compiler translates the whole program at once and creates an executable that can run without it. An interpreter translates and runs one statement at a time, an assembler works on assembly language rather than high-level code, and a loader only places an executable in memory. - In a flowchart, a step that asks a Yes/No question is drawn as a:
A) Parallelogram
B) Rectangle
C) Oval
D) Diamond
Answer: D) Diamond β Decisions with two or more branches use the diamond symbol. A parallelogram is for input and output, a rectangle for processing, and an oval for the start and end of the flowchart. - Which of the following is usually classed as a fourth-generation language (4GL)?
A) C++
B) SQL
C) FORTRAN
D) Assembly language
Answer: B) SQL β SQL is non-procedural: the user states what data is wanted, not how to find it, which is the mark of a 4GL. C++ and FORTRAN are third-generation procedural languages, and assembly language is second generation. - A program stops while running because it tries to divide a number by zero. This is a:
A) Syntax error
B) Runtime error
C) Logical error
D) Linker error
Answer: B) Runtime error β Division by zero can only be discovered during execution, so it is a runtime error. A syntax error is caught before running, a logical error lets the program finish with a wrong answer, and a linker error occurs while joining object files. - A program runs to the end without any error message but prints the wrong total because a minus sign was used instead of a plus. This is a:
A) Syntax error
B) Runtime error
C) Compilation error
D) Logical error
Answer: D) Logical error β The instructions are valid and nothing crashes, yet the result is wrong, which defines a logical error. A syntax or compilation error would be reported during translation, and a runtime error would stop the program while running. - Which data type is used to store only the values true or false?
A) Boolean
B) Integer
C) Character
D) Float
Answer: A) Boolean β A Boolean variable holds just two values, true and false. Integer stores whole numbers, character stores a single symbol, and float stores numbers with a decimal part. - What is printed? a β 17; b β 5; c β (a MOD b) + (a DIV b); PRINT c
A) 3
B) 2
C) 5
D) 6
Answer: C) 5 β 17 MOD 5 is the remainder 2 and 17 DIV 5 is the whole quotient 3, so c = 2 + 3 = 5. The value 3 is the quotient alone and 2 is the remainder alone; 6 does not arise from these operations. - What is printed? n β 1; WHILE n β€ 20: n β n Γ 2; END WHILE; PRINT n
A) 32
B) 16
C) 20
D) 64
Answer: A) 32 β n doubles while it is at most 20: 1, 2, 4, 8, 16, then 16 β€ 20 is still true so n becomes 32. Now 32 β€ 20 is false and the loop ends, printing 32. Stopping at 16 forgets the last pass, 20 is only the limit, and 64 would need one extra pass. - Bundling data with the methods that use it inside a class, and hiding that data from direct outside access, is called:
A) Inheritance
B) Polymorphism
C) Abstraction
D) Encapsulation
Answer: D) Encapsulation β Encapsulation wraps data and methods together and protects the data, usually by making it private. Inheritance passes features from a parent class to a child, polymorphism lets one name take many forms, and abstraction shows only essential features while hiding complexity. - Class Dog inherits speak() from class Animal and then defines its own speak() with the same parameters. This is an example of:
A) Encapsulation
B) Method overloading
C) Method overriding
D) Abstraction
Answer: C) Method overriding β A child class replacing an inherited method that has the same name and parameters is method overriding, a form of run-time polymorphism. Overloading uses the same name with different parameters, while encapsulation and abstraction are about hiding data and detail, not redefining methods. - Which loop always executes its body at least once, whatever the condition?
A) WHILE loop
B) DO-WHILE (REPEAT-UNTIL) loop
C) FOR loop
D) Nested IF
Answer: B) DO-WHILE (REPEAT-UNTIL) loop β A DO-WHILE or REPEAT-UNTIL loop checks its condition after the body, so one pass is guaranteed. A WHILE loop checks first and may run zero times, a FOR loop can also run zero times if its range is empty, and a nested IF is a selection structure, not a loop. - What is printed? marks β 72; IF marks β₯ 80 THEN PRINT "A" ELSE IF marks β₯ 60 THEN PRINT "B" ELSE PRINT "C"
A) A
B) C
C) Nothing is printed
D) B
Answer: D) B β 72 β₯ 80 is false, so the program moves to the next test; 72 β₯ 60 is true, so it prints B and skips the final ELSE. A would need 80 or more, C would need marks below 60, and one branch always prints, so the output cannot be empty. - Which of these is a valid variable name in most programming languages?
A) total_marks
B) 2ndValue
C) my value
D) for
Answer: A) total_marks β total_marks starts with a letter and uses only letters and an underscore, so it is valid. 2ndValue starts with a digit, my value contains a space, and for is a reserved keyword, so none of them can be used as a name. - How many times is HELLO printed? FOR i β 1 TO 3: FOR j β 1 TO 4: PRINT "HELLO"; END FOR; END FOR
A) 7
B) 4
C) 12
D) 3
Answer: C) 12 β The inner loop prints 4 times for every pass of the outer loop, and the outer loop runs 3 times, so 3 Γ 4 = 12. Adding the counts gives 7, and 4 or 3 counts only one of the loops.
Questions
What is the difference between a compiler and an interpreter?
A compiler translates the whole program into machine code before running it and produces an executable, so the program runs fast and the compiler is not needed later. An interpreter translates and runs one statement at a time, stops at the first error and is needed every time the program runs.
What are the four pillars of OOP?
Encapsulation (bundling and hiding data), abstraction (showing only essential features), inheritance (a child class reusing a parent class) and polymorphism (one name, many forms, through overloading and overriding).
Which flowchart symbol is used for input and output?
The parallelogram. The oval marks start and end, the rectangle a process and the diamond a decision.
Where does the word algorithm come from?
It comes from the name of the 9th-century Persian mathematician Muhammad ibn Musa al-Khwarizmi, whose works on arithmetic and algebra were translated into Latin.