All Exam Answers - Introduction to Programming
Mid Term Feb 2019
Q1:
1. Keywords are reserved (e.g., int, return). Identifiers are user-defined names (e.g., age). Identifiers
must not be keywords, must begin with a letter/underscore, and cannot have special characters.
2. Steps: 1. Preprocessing -> 2. Compilation -> 3. Assembling -> 4. Linking -> 5. Execution
3. i) double
ii) unsigned long long
iii) char
iv) float
4. Precedence defines operation order (* over +). Associativity determines direction (e.g., left-to-right
for +, -).
5. Header files:
i) stdio.h - input/output
ii) math.h - math functions
iii) string.h - string operations
Q2:
1. Operators in C: Arithmetic (+,-), Relational (==, !=), Logical (&&, ||), Bitwise (&, |), Assignment (=,
+=) etc.
2. i) j = 7 + 8*8 = 71
ii) Invalid: a=5<=8 && 6=5 (should be ==)
iii) -10%-3 = -1
iv) Needs full expression
v) Depends on previous vars
Q3:
1. Program:
int sum = 0;
for (int i = 100; i <= 1000; i++) {
if (i % 7 == 0) { printf("%d ", i); sum += i; } }
printf("\nSum = %d", sum);
2. Switch: Used for fixed choices. Disadvantages: no ranges, only int/char.
Example: switch(menu) { case 1: ... }
Q4:
1. Break exits loop; Continue skips current iteration.
Example:
for(i=0;i<5;i++) { if(i==3) continue; printf("%d",i);}
2. getchar() reads a char; putchar() prints a char.
Example: char c = getchar(); putchar(c);
End Term May-June 2014
Q1:
1. a) 2's complement: M=101000100, N=00110010 => Convert N to 2s complement and add to M.
Result: M - N = 011100010
2. b) Design Flow: Understand problem -> Create Algorithm -> Draw Flowchart -> Code -> Compile
-> Test
3. c) Flowchart is graphical; Data Flow Diagram shows data movement.
4. d) ASCII (7-bit), EBCDIC (8-bit IBM). ASCII is standard, EBCDIC is less common.
5. e) Operator: symbol for operation. Types: Arithmetic (+), Relational (==), Logical (&&), Bitwise (&),
Assignment (=).
UNIT-I:
1. Algorithm: Step-by-step solution. Features: Finiteness, Definiteness, Input/Output, Effectiveness.
Format: start, steps, end.
2. Design techniques: Top-down, bottom-up, modular design, pseudocode usage.
UNIT-II:
1. Precedence = priority of operators, Associativity = direction (left-right).
Quadratic roots program:
if D>0 real roots,
D==0 one root,
D<0 imaginary roots.
Q5:
1. a) Formatted I/O: printf, scanf. Unformatted: getchar, putchar.
2. b) for loop = known iteration, while = entry-controlled, do-while = exit-controlled.
UNIT-III:
1. Entry Controlled: while, for
Exit Controlled: do-while
2. Print pattern: Use nested for loops with rows and columns.
UNIT-IV:
1. a) Function returns int by default. Use return_type func_name().
2. b) Format Arg = %d, Actual Arg = passed in call, Declaration = prototype, Definition = with body
Q8:
1. a) Static = fixed memory. Dynamic = malloc/free.
2. b) Call by Value = copy passed. Reference = address passed.
3. c) Array of char = string. Pointer to char = dynamic string.
4. d) Structure = heterogeneous. Union = shares memory.
Q9:
1. a) Pointer to Const = value can't change. Const Pointer = address can't change.
2. b) Sorting strings: Use strcmp, strcpy in loop.