








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









MASTER PROGRAMMING FUNDAMENTALS, CONTROL FLOW, FUNCTIONS, DATA STRUCTURES, AND SCRIPTING AUTOMATION WITH APPLIED EXERCISES Super-Index Programming Fundamentals o Variables, Data Types, Constants, Operators o Input/Output Basics Control Flow o if / else / switch o for, while, do-while loops o Nested conditions, break & continue Functions & Modular Programming o Defining and calling functions o Parameters & return values o Scope & lifetime Data Structures Basics o Arrays / Lists o Dictionaries / Maps
o Intro to Stacks & Queues Scripting & Automation Basics o File I/O o Simple scripts for automation o Basic error handling Debugging & Best Practices o Syntax & logic error identification o Code comments & clean coding Practice Exam Sections o Section 1: Easy – Fundamentals & Syntax o Section 2: Moderate – Control Flow & Functions o Section 3: Advanced – Multi-step & Applied Scripting NGN Applied Mini-Cases (10 Cases) o Real-world multi-step coding problems o Emphasize logic, debugging, and applied reasoning Concept Crash Notes Variables & Data Types Integers, floats, strings, booleans Constants vs mutable variables Control Flow if / else evaluates boolean conditions Loops repeat code blocks while condition is true
A) No B) Yes C) Error D) None Answer: B Rationale: 7 > 5 → “Yes” printed. Q3. Which symbol is used for exponentiation in Python? A) ^ B) ** C) % D) // Answer: B Rationale: ** performs power calculations. Q4. What type is returned by input() function? A) Integer B) Float C) String D) Boolean Answer: C Rationale: input() always returns a string. Q5. Multi-step applied scenario: Task: Write a script that asks for user age and prints “Adult” if ≥18, else “Minor”. Rationale: Tests input handling, type casting, and conditional statements. Section 2 — Control Flow & Functions (Q16–Q35) Q16. What is the output? for i in range(3): print(i) A) 1 2 3 B) 0 1 2 C) 0 1 2 3 D) 1 2 3 4 Answer: B Rationale: range(3) produces 0,1,2. Q17. Break statement is used to: A) Skip current iteration B) Exit the loop C) Pause execution D) None
Answer: B Rationale: break immediately terminates the loop. Q18. What does this function return? def add(a, b): return a + b A) Sum of a and b B) Product of a and b C) None D) a – b Answer: A Rationale: return a + b outputs the sum. Q19. Multi-step scenario: Task: Write a function that takes a list of numbers and returns only the even numbers. Rationale: Combines loops, conditionals, function definition, return values. Section 3 — Applied Scripting & Advanced (Q36–Q50) Q36. Which Python structure allows key-value pairing? A) List B) Tuple C) Dictionary D) Set Answer: C Rationale: Dictionaries store data as key-value pairs. Q37. Output of: x = 10 y = 3 print(x // y) A) 3.33 B) 3 C) 3.0 D) 4 Answer: B Rationale: // is floor division → 10 // 3 = 3. Q38. Multi-step applied scenario: Task: Script that reads a text file and counts the number of lines containing “Python”.
Q44. What is the output of: for i in range(2): for j in range(3): print(i, j) A) (0,0),(0,1),(0,2),(1,0),(1,1),(1,2) B) (0,0),(1,0),(2,0) C) (0,0),(0,1),(1,0),(1,1) D) Error Answer: A Rationale: Nested loops execute the inner loop completely for each iteration of the outer loop. Q45. How do you catch multiple exceptions in a single except clause? A) except (TypeError, ValueError): … B) except TypeError, ValueError: C) catch (TypeError | ValueError): … D) Python cannot catch multiple exceptions in one clause Answer: A Rationale: Use parentheses to list multiple exceptions in a single except. Q46. Multi-step scenario: Task: Automate renaming files in a folder to append _2026 to filenames. Include error handling if file is not found. Rationale: Tests loops, string manipulation, file I/O, and exception handling. Q47. Which Python method removes whitespace from the beginning and end of a string s? A) s.strip() B) s.ltrim() C) s.trim() D) s.clean() Answer: A Rationale: .strip() removes leading and trailing whitespace.
Q48. Multi-step scenario: Task: Script that counts how many times each word appears in a given text file and prints the top 3 most frequent words. Rationale: Combines loops, dictionaries, string splitting, sorting, and applied reasoning. Q49. Which statement best describes a nested loop? A) A loop inside another loop B) A loop that runs twice C) A loop that skips iterations D) A loop inside a function Answer: A Rationale: Nested loops execute the inner loop fully for each iteration of the outer loop. Q50. Multi-step scenario: Task: Write a script that reads a CSV file of sales data, calculates total sales per region, and outputs regions sorted by highest sales. Rationale: Combines file I/O, loops, dictionaries/maps, aggregation, and sorting — simulating a real-world applied coding problem. NGN Applied Coding Mini-Cases Case 1 — FizzBuzz Automation Scenario: Write a script that prints numbers 1–20. For multiples of 3, print “Fizz”; for multiples of 5, print “Buzz”; for multiples of both, print “FizzBuzz”. Solution (Python): for i in range(1, 21): if i % 3 == 0 and i % 5 == 0: print("FizzBuzz")
files = [f for f in os.listdir('.') if f.endswith('.txt')] for file in files: try: os.rename(file, file.replace('.txt','_backup.txt')) except FileNotFoundError: print(f"File {file} not found.") Rationale: Tests file I/O, loops, string manipulation, and exception handling. Case 4 — Count Word Occurrences Scenario: Count how many times each word appears in a text string. Solution: text = "Python is fun and Python is powerful" word_count = {} for word in text.split(): word_count[word] = word_count.get(word, 0) + 1 print(word_count) Rationale: Combines loops, dictionaries, string methods, and counting logic. Case 5 — Palindrome Checker
Scenario: Check if a user-input string is a palindrome. Solution: s = input("Enter string: ").lower().replace(" ", "") if s == s[::-1]: print("Palindrome") else: print("Not Palindrome") Rationale: Tests string manipulation, slicing, conditional logic, and input handling. Case 6 — Simple Calculator Function Scenario: Create a function calculator(a, b, op) that performs +, -, , / operations and handles division by zero. Solution: def calculator(a, b, op): try: if op == '+': return a + b elif op == '-': return a - b elif op == '': return a * b elif op == '/': return a / b except ZeroDivisionError:
reader = csv.reader(file) for row in reader: scores.append(int(row[1])) # Assuming second column is score print(f"Average: {sum(scores)/len(scores)}, Max: {max(scores)}, Min: {min(scores)}") Rationale: Combines file I/O, loops, type casting, aggregation , and applied reasoning. Case 9 — Daily Temperature Logger Scenario: Script reads temperatures for a week and prints days above 30°C. Solution: temps = [28, 32, 31, 29, 35, 30, 27] for i, temp in enumerate(temps, start=1): if temp > 30: print(f"Day {i}: {temp}°C") Rationale: Multi-step logic loops, enumeration, conditionals, indexing. Case 10 — Simple Login System
Scenario: Check username/password input against predefined credentials. Solution: username = input("Username: ") password = input("Password: ") if username == "admin" and password == "1234": print("Access Granted") else: print("Access Denied") Rationale: Tests input handling, conditionals, multi-step logic, and authentication concept.