Top Banner Ad
ACS101 · 1-Page Cheat Sheet

Programming Languages

ACS101 · YEAR 1 · SEM 1

Programming Languages — Last-Night Cheat Sheet

prephora.in

Four units of C and two of Python: for every question, show the declaration/statement, trace the values, and write the exact output — the examiner marks the trace, not the explanation.

1. Programming Basics (C)

Recall anchor. Declare, initialise, operate, print: a C program is a sequence of typed statements, and every operator result depends on operand type.

Scored points.

  • Structure of a C program: preprocessor directive (#include <stdio.h>), global declarations, main() with int return type, declarations, statements, and return 0.
  • Compilation and execution: source (.c) → preprocessor → compiler → object (.obj) → linker → executable (.exe); gcc file.c -o file then run.
  • Data types, variables and constants: int, char, float, double, void have type-dependent ranges; identifiers follow naming rules, const or #define fixes a value, and uninitialised locals contain garbage.
  • Operators: arithmetic (+ − × / %), relational (< > <= >= == !=), logical (&& || !), assignment, and increment/decrement; % works only on integers.
  • Input/output: scanf("%d", &x) needs the address-of operator; printf("%d", x) prints the value; format specifiers %d, %f, %c, %s must match the type.
  • Type conversion: implicit promotion in mixed expressions (int → float → double); explicit cast (float)a/b changes integer division into real division.
  • Real connect: the same typed, close-to-hardware model powers embedded systems, microcontrollers, and OS kernels.

Formula strip.

program=#include+declarations+main(){statements},source→compileobject→linkexe,&x for scanf, x for printf\text{program}=\#\text{include}+\text{declarations}+\text{main()}\{\text{statements}\},\quad \text{source}\xrightarrow{\text{compile}}\text{object}\xrightarrow{\text{link}}\text{exe},\quad \&x\ \text{for scanf},\ x\ \text{for printf}

Micro-example. int a=7,b=2: a/b = 3 (integer division), a%b = 1; with a cast, (float)a/b = 3.5.

Examiner line. Write the full compilable skeleton, then trace one expression with the type written beside each intermediate value.

2. Decision Making and Control Flow (C)

Recall anchor. Condition chooses, loop repeats, function packages: recursion is just a function that calls itself on a smaller input until the base case stops it.

Scored points.

  • Control statements: if, if–else, else–if ladder, switch with case and break, and the conditional operator ?: ; missing break causes fall-through.
  • Loops: for(init; test; update) executes zero or more times; while tests first; do–while executes at least once.
  • break exits the nearest loop or switch; continue skips the rest of the current iteration; infinite loops arise when the update never changes the test.
  • Functions: definition (return type, name, parameter list, body), prototype, call, and return; call by value passes a copy, so the caller's variable is unchanged.
  • Recursion: factorial(n) = n × factorial(n−1) with factorial(0)=1; Fibonacci(n) = Fibonacci(n−1)+Fibonacci(n−2) with bases 0 and 1; every recursive function needs a base case and a converging step.
  • Scope and lifetime: local variables are recreated on each call; a recursive call gets its own stack frame.
  • Real connect: control flow implements traffic-signal logic and drone autopilot decisions; tutorial tasks include largest-of-three, grade calculator, and day-name using switch-case.

Formula strip.

for(i=0;i<n;i++)≡i=0; while(i<n){...;i++},n!=n(n−1)!, f(0)=1,Fn=Fn−1+Fn−2 (F0=0,F1=1)\text{for}(i=0;i<n;i++)\equiv i=0;\ \text{while}(i<n)\{...;i++\},\quad n!=n(n-1)!,\ f(0)=1,\quad F_n=F_{n-1}+F_{n-2}\ (F_0=0,F_1=1)

Micro-example. factorial(5) = 5 × 4 × 3 × 2 × 1 = 120; Fibonacci terms 0,1,1,2,3,5,8 give F₆ = 8; the loop sum 1+2+3+4+5 = 15.

Examiner line. State the condition or base case first, trace two iterations or two recursive calls, then give the final printed value in one line.

3. Advanced Programming Concepts (C)

Recall anchor. Index the array, terminate the string, open the file: these three mechanics cover most of the C advanced questions.

Scored points.

  • 1D array: declaration int a[10], input in a loop, output by index, and traversal from a[0] to a[n−1]; out-of-bounds indexing is undefined behaviour.
  • Simple array operations: maximum, minimum, sum, and average; linear search checks each element in order.
  • Strings in C: char arrays terminated by '\0'; gets/puts read and print lines, strlen counts characters before the terminator, strcpy copies, strcmp compares lexicographically.
  • Pointer concept: a pointer variable stores an address; &x gives the address, *p accesses the value at that address; concept only, no deep pointer arithmetic needed.
  • Structures: struct groups related members of different types; access with the dot operator, and pass by pointer where modification is needed.
  • File handling: FILE *fp = fopen("data.txt", "r"/"w"), fscanf/fprintf or fgetc/fputc, and fclose; EOF check prevents processing past the end.
  • Real connect: arrays store sensor readings, image pixels, and stock prices; pointers underpin memory handling in embedded systems.

Formula strip.

&a[i]=base+i×sizeof(type),strlen(s)=#chars before ′ ⁣\0′,fp=fopen(name,mode),strcmp=0⇔equal\&a[i]=\text{base}+i\times\text{sizeof(type)},\quad \text{strlen}(s)=\#\text{chars before }'\!\backslash0',\quad fp=\texttt{fopen(name,mode)},\quad \text{strcmp}=0\Leftrightarrow\text{equal}

Micro-example. For int a[5] starting at address 1000 with 4-byte ints, &a[3] = 1000 + 3×4 = 1012; strlen("CSE") = 3 although the array occupies 4 bytes.

Examiner line. Show the declaration, the loop bounds, and the terminating condition ('\0' or EOF); then print the traced result with its exact format specifier.

4. Python Introduction

Recall anchor. Indentation replaces braces: Python keeps the same ideas as C but removes declarations, semicolons, and explicit types.

Scored points.

  • Why Python: dominant in AI, data science, web, and automation; same logic as C with less boilerplate and readable syntax.
  • Basics: variables get types from the assigned value; int, float, str, bool; print() displays and input() reads a string (cast with int() or float()).
  • Operators: + − * / // % **; / gives float, // gives floor division, ** is exponentiation; + concatenates strings.
  • Control flow: if/elif/else with a colon and indentation; for x in range(n) for counted loops, while for condition loops; no braces.
  • Functions: def name(params): with return; default arguments such as def f(a, b=2) allow fewer arguments; no explicit return type.
  • Comparison with C: no compile step, no & for input, no format specifiers, dynamic typing; errors appear at runtime.
  • Tutorial tasks: BMI calculator, temperature converter (°C ↔ °F), grade calculator, Fibonacci loop, and calculator using functions.

Formula strip.

def f(a,b=2): return a∗b,for x in range(n)≡x=0…n−1,x=int(input()),a//b=⌊a/b⌋\texttt{def}\,f(a,b=2):\,\texttt{return}\,a*b,\quad \texttt{for}\ x\ \texttt{in range}(n)\equiv x=0\ldots n-1,\quad x=\texttt{int(input())},\quad a//b=\lfloor a/b\rfloor

Micro-example. def area(r, h=2): return 3.14rh; area(3) = 3.14 × 3 × 2 = 18.84, and area(3, 4) = 37.68.

Examiner line. Give the Python statement in exact syntax (colon, indentation, no semicolon) and write the printed value; compare with the C form only when asked.

5. Python Data Structures and Emerging Applications

Recall anchor. Choose the container by how you access it: list by index, dictionary by key, set for uniqueness, NumPy for vector maths.

Scored points.

  • Data structures: list [] is ordered and mutable; tuple () is ordered and immutable; dictionary {key: value} maps keys to values; set {1, 2} stores unique values.
  • Basic operations: append/index/slice for lists, len() for size, in for membership, dict[key] lookup, and set union/intersection.
  • File handling: open(name, "r"/"w") with read(), readline(), write(), and with-blocks; a CSV is read by splitting each row on the comma.
  • NumPy: np.array([...]) creates an array, np.mean(a) gives the average, and element-wise arithmetic replaces explicit loops.
  • Matplotlib: plt.plot(x, y), plt.xlabel(), plt.ylabel(), plt.show() produce a line graph for a report.
  • Mini-demo: simulate five IoT temperature readings in a list, compute the mean, and plot them — the same Python basics lead into AI, IoT, and data science.
  • Tutorial tasks: max/min/average of five marks, dictionary of names and marks, word frequency, student records to a text file, and a small CSV print.

Formula strip.

list=[x1,…,xn],dict={ki:vi},xˉ=np.mean(a)=1n∑i=1nxi,plt.plot(x,y)\text{list}=[x_1,\dots,x_n],\quad \text{dict}=\{k_i:v_i\},\quad \bar x=\texttt{np.mean}(a)=\frac1n\sum_{i=1}^n x_i,\quad \texttt{plt.plot}(x,y)

Micro-example. marks = [70, 80, 90]: max = 90, min = 70, np.mean(marks) = (70+80+90)/3 = 80; dict {'A': 90, 'B': 80}['A'] = 90.

Examiner line. Name the container before coding, show one operation on it, and print the resulting value; for plots state the axes labels as part of the answer.

Memory map

%%{init: {'theme': 'base', 'look': 'handDrawn', 'themeVariables': {'fontFamily': "'Nunito', sans-serif", 'fontSize': '15px', 'primaryColor': '#EFEAD6', 'primaryBorderColor': '#0F766E', 'lineColor': '#23211C'}}}%%
flowchart LR
  A["U1: Structure of a C program"] --> B["U1: Data types"] --> C["U1: operators (arithmetic, relational, logical)"]
  C --> D["U2: Control Statements"] --> E["U2: Loops — for, while, do-while"] --> F["U2: Recursion — factorial and Fibonacci"]
  F --> G["U3: 1D array — declaration, input, output, traversal"] --> H["U3: Strings in C — strlen, strcpy, strcmp"]
  H --> I["U3: File Handling"] --> J["U4: Python basics — variables, data types, print, input"]
  J --> K["U4: Python functions — def, return, default arguments"] --> L["U5: Python list, tuple, dictionary, set"]
  L --> M["U5: File handling — read and write a text file"] --> N["U5: Introduction to NumPy"]
  N --> O["U5: Introduction to Matplotlib"]

Rapid recall

  1. U1 · Structure of a C program
  2. U1 · compilation and execution
  3. U1 · Data types
  4. U1 · variables
  5. U1 · constants
  6. U1 · operators (arithmetic, relational, logical)
  7. U1 · Input/output — scanf/printf in C
  8. U1 · Type conversion
  9. U1 · simple expressions
  10. U2 · Control Statements
  11. U2 · Loops — for, while, do-while
  12. U2 · break and continue
  13. U2 · Functions — definition, call, return, call by value
  14. U2 · Recursion — factorial and Fibonacci
  15. U3 · 1D array — declaration, input, output, traversal
  16. U3 · Simple operations — find max, min, sum, average
  17. U3 · Strings in C — gets/puts, strlen, strcpy, strcmp
  18. U3 · Pointer — concept of address and pointer variable
  19. U3 · Structures
  20. U3 · File Handling
  21. U4 · Why Python — AI, Data Science, Web, Automation
  22. U4 · Python basics — variables, data types, print, input, operators
  23. U4 · Python control flow — if/else, for loop, while loop
  24. U4 · Python functions — def, return, default arguments
  25. U5 · Python list, tuple, dictionary, set
  26. U5 · File handling — read and write a text file
  27. U5 · read a CSV
  28. U5 · Introduction to NumPy — array, mean, simple arithmetic
  29. U5 · Introduction to Matplotlib — plot a simple line graph
  30. U5 · One mini-demo — IoT sensor simulation

Final-answer checklist

  • For ACS101, produce code that compiles or runs; balance every brace in C and indent every Python block.
  • Match format specifiers in scanf/printf and cast explicitly when a real result is required.
  • Trace two loop iterations or two recursive calls before stating the final output.
  • Use the exact library call for the task: strlen/strcpy/strcmp, fopen/fclose, np.mean, or plt.plot.
  • End with the output in a printed line (e.g., "Output: 120"), not a description of the output.