0% found this document useful (0 votes)
9 views25 pages

Informatics Midterm Exam Practice Variants

Uploaded by

lizi.zarn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views25 pages

Informatics Midterm Exam Practice Variants

Uploaded by

lizi.zarn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Informatics 1

Midterm Exam - Practice Material

This document contains four possible variants of a midterm test, designed


to be similar in structure and scope to the official exam.
Compiled by Andria Gvaramia.

General Instructions for All Variants:

• Format: This is a paper-based exam. You are not required to write runnable code.

• Questions: Answer all 5 questions.

• Grading: Q1=20 pts, Q2=15 pts, Q3=15 pts, Q4=25 pts, Q5=25 pts. (Total: 100
points)

• Scope: The material covers Lectures 1-25.

• Work: Show your work for tracing questions to receive partial credit.

1
Variant A

Question 1: Code Tracing and Fundamentals (20 points)


For each of the following code snippets, determine the final output.

(a) (5 points)
1 x = 10
2 y = 4
3 result = x // y + ( x % y ) * 2
4 print ( result )

Output:

(b) (5 points)
1 my_string = " informatics "
2 s1 = my_string [1:5]
3 s2 = my_string [ -1]
4 print ( s1 , s2 )

Output:

(c) (5 points)
1 my_list = [10 , 20 , 30]
2 my_list . append (40)
3 my_list [1] = 5
4 print ( my_list )

Output:

2
(d) (5 points)
1 age = 20
2 has_id = False
3 if age >= 18 and has_id :
4 print ( " Allowed " )
5 elif age >= 18 and not has_id :
6 print ( " ID Required " )
7 else :
8 print ( " Not Allowed " )

Output:

3
Question 2: Control Flow Analysis (15 points)
Trace the execution of the following code. Show the value of the ‘count‘ variable and the
output at each iteration.
1 count = 0
2 for i in range (1 , 10) :
3 if i % 2 == 0:
4 continue
5 if i > 5:
6 break
7 count = count + i
8 print ( " Current count : " , count )
9

10 print ( " Final count : " , count )

Trace Table and Final Output:

Question 3: Recursion Tracing (15 points)


Trace the execution of ‘factorial(4)‘. Show each recursive call and the value it returns.
The factorial function is defined as n! = n × (n − 1)! and 0! = 1.
1 def factorial ( n ) :
2 if n == 0:
3 return 1
4 else :
5 return n * factorial ( n - 1)

Execution Trace for factorial(4):

4
Question 4: Data Structures and Algorithms (25 points)
(a) (10 points)
Describe an algorithm using **pseudocode** that finds the most frequent element in a
list of numbers. Your algorithm should use a dictionary to store frequencies.
Pseudocode:

(b) (15 points)


Trace the execution of the following code for the input nums = [1, 3, 2, 3, 1, 3].
Show the state of freq dict after the first loop finishes and write the final output.
1 def find_m os t_f re qu en t ( numbers ) :
2 freq_dict = {}
3 for num in numbers :
4 if num in freq_dict :
5 freq_dict [ num ] += 1
6 else :
7 freq_dict [ num ] = 1
8

9 max_count = 0
10 most_frequent = None
11 for key , value in freq_dict . items () :
12 if value > max_count :
13 max_count = value
14 most_frequent = key
15

16 print ( f " Most frequent : { most_frequent } , Count : { max_count } " )


17

18 find_most_ fr eq ue nt ([1 , 3 , 2 , 3 , 1 , 3])

Trace and Final Output:

5
Question 5: Object-Oriented Programming Concepts
(25 points)
Consider the following Python classes:
1 class Vehicle :
2 def __init__ ( self , make , year ) :
3 self . make = make
4 self . year = year
5

6 def display_info ( self ) :


7 print ( f " { self . year } { self . make } " )
8

9 class Car ( Vehicle ) :


10 def __init__ ( self , make , year , doors ) :
11 super () . __init__ ( make , year )
12 self . doors = doors
13

14 def display_info ( self ) :


15 print ( f " { self . year } { self . make } , { self . doors } doors " )

(a) (10 points)


Answer the following conceptual questions:

1. Identify the parent class and the child class.

2. What is the purpose of the super(). init (make, year) line?

3. What OOP concept is demonstrated by the display info method in the Car class?
Explain it.

4. List all attributes of an object created from the Car class.

(b) (15 points)


Determine the output of the following code snippet.

6
1 v1 = Vehicle ( " Toyota " , 2020)
2 c1 = Car ( " Ford " , 2022 , 4)
3

4 v1 . display_info ()
5 c1 . display_info ()

Output:

7
Variant B

Question 1: Code Tracing and Fundamentals (20 points)


For each of the following code snippets, determine the final output.

(a) (5 points)
1 a = 5
2 b = 2
3 result = ( a ** b ) - ( b * 5)
4 print ( result )

Output:

(b) (5 points)
1 text = " Kutaisi "
2 part1 = text [0] + text [ -3:]
3 print ( part1 )

Output:

(c) (5 points)
1 my_tuple = (10 , 20 , 30 , 40)
2 my_list = list ( my_tuple )
3 my_list . pop (2)
4 print ( tuple ( my_list ) )

Output:

8
(d) (5 points)
1 score = 85
2 if score < 60:
3 print ( " Fail " )
4 elif score < 90:
5 print ( " Pass " )
6 else :
7 print ( " Excellent " )

Output:

9
Question 2: Control Flow Analysis (15 points)
Trace the execution of the following code. Show the value of x and the output for each
iteration.
1 x = 10
2 while x > 0:
3 print ( x )
4 x = x - 3
5 if x == 4:
6 break

Trace Table and Output:

Question 3: Recursion Tracing (15 points)


Trace the execution of fib(4). Show each recursive call and its return value. The
Fibonacci sequence is defined as F (n) = F (n − 1) + F (n − 2), with base cases F (0) = 0
and F (1) = 1.
1 def fib ( n ) :
2 if n <= 1:
3 return n
4 else :
5 return fib ( n - 1) + fib ( n - 2)

Execution Trace for fib(4):

10
Question 4: Data Structures and Algorithms (25 points)
(a) (10 points)
Describe an algorithm using **pseudocode** to check if a string is a palindrome, ignoring
case. It should compare characters from the start and end, moving inwards.
Pseudocode:

(b) (15 points)


Trace the execution of the following code for the input text = "Racecar". Show the
values of left, right, and is palindrome during the loop.
1 def is_palindrome ( s ) :
2 s_lower = s . lower ()
3 left = 0
4 right = len ( s_lower ) - 1
5 is_palindrome = True
6

7 while left < right :


8 if s_lower [ left ] != s_lower [ right ]:
9 is_palindrome = False
10 break
11 left += 1
12 right -= 1
13

14 print ( f " Is ’{ s } ’ a palindrome ? { is_palindrome } " )


15

16 is_palindrome ( " Racecar " )

Trace and Final Output:

11
Question 5: Object-Oriented Programming Concepts
(25 points)
Consider the following Python class:
1 class Student :
2 university = " KIU " # Class variable
3

4 def __init__ ( self , name , student_id ) :


5 self . name = name # Instance variable
6 self . __id = student_id # Private instance variable
7

8 def get_id ( self ) :


9 return self . __id
10

11 @classmethod
12 def get_university ( cls ) :
13 print ( f " University : { cls . university } " )

(a) (10 points)


Answer the following conceptual questions:

1. Identify one class variable and one instance variable. What is the key difference
between them?

2. The id attribute is private. What does this mean in the context of encapsulation?

3. The get university method is a class method. What does the cls parameter refer
to?

4. Can an instance method (like a hypothetical display info(self) method) access


a class variable? If so, how?

(b) (15 points)


Determine the output of the following code snippet.

12
1 s1 = Student ( " Luka " , 12345)
2 s2 = Student ( " Anna " , 67890)
3

4 print ( s1 . name )
5 print ( s2 . get_id () )
6

7 Student . university = " New University "


8 s1 . get_university ()

Output:

13
Variant C

Question 1: Code Tracing and Fundamentals (20 points)


For each of the following code snippets, determine the final output.

(a) (5 points)
1 a = 3
2 b = 3.0
3 print ( a == b , type ( a ) is type ( b ) )

Output:

(b) (5 points)
1 my_set = {1 , 2 , 3}
2 my_set . add (3)
3 my_set . add (4)
4 print ( len ( my_set ) )

Output:

(c) (5 points)
1 grades = { " math " : 90 , " history " : 85}
2 grades [ " history " ] = 88
3 grades [ " science " ] = 92
4 print ( grades [ " science " ])

Output:

14
(d) (5 points)
1 x = 5
2 y = 10
3 if x > 5 or y == 10:
4 print ( " Success " )
5 else :
6 print ( " Failure " )

Output:

15
Question 2: Control Flow Analysis (15 points)
Trace the execution of the following nested loops and write down everything that is
printed.
1 for i in range (1 , 4) :
2 for j in range ( i ) :
3 print (i , end = " " )
4 print () # prints a new line

Output:

Question 3: Recursion Tracing (15 points)


Trace the execution of sum down(3). Show each recursive call and the value it returns.
1 def sum_down ( n ) :
2 if n == 0:
3 return 0
4 else :
5 return n + sum_down ( n - 1)

Execution Trace for sum down(3):

16
Question 4: Data Structures and Algorithms (25 points)
(a) (10 points)
Describe the improved **linear search algorithm for a sorted list**. Explain why it can
be more efficient than a linear search on an unsorted list in the average case, and state
its worst-case time complexity.
Algorithm Description and Explanation:

(b) (15 points)


Trace the execution of the following code for ‘data = [10, 20, 30, 40, 50]‘ and ‘target
= 35‘. Show the output of each ‘print‘ statement inside the loop and explain the final
result.
1 def sorte d _ l i n e a r _ s e a r c h ( data , target ) :
2 for i in range ( len ( data ) ) :
3 print ( f " Checking index { i } , value { data [ i ]} " )
4 if data [ i ] == target :
5 return True
6 if data [ i ] > target :
7 return False
8 return False
9

10 result = s o r t e d _ l i n e a r _ s e a r c h ([10 , 20 , 30 , 40 , 50] , 35)


11 print ( " Result : " , result )

Trace and Final Output:

17
Question 5: Object-Oriented Programming Concepts
(25 points)
Consider the following Python classes:
1 class Animal :
2 def __init__ ( self , name ) :
3 self . name = name
4

5 def speak ( self ) :


6 print ( " Some generic sound " )
7

8 class Dog ( Animal ) :


9 def speak ( self ) :
10 print ( " Woof ! " )
11

12 class Cat ( Animal ) :


13 def speak ( self ) :
14 print ( " Meow ! " )

(a) (10 points)


Answer the following conceptual questions:

1. What type of inheritance is demonstrated here (Single, Multiple, Hierarchical, etc.)?

2. Explain method overriding using the speak method as an example.

3. If we create a Dog object, which init method is called? Why?

4. Can an object of class Cat access the name attribute? Explain why or why not.

(b) (15 points)


Determine the output of the following code snippet.
1 generic_animal = Animal ( " Creature " )
2 my_dog = Dog ( " Buddy " )

18
3 my_cat = Cat ( " Whiskers " )
4

5 generic_animal . speak ()
6 my_dog . speak ()
7 my_cat . speak ()
8 print ( my_dog . name )

Output:

19
Variant D

Question 1: Code Tracing and Fundamentals (20 points)


For each of the following code snippets, determine the final output.

(a) (5 points)
1 x = "2"
2 y = 3
3 print ( x * y )

Output:

(b) (5 points)
1 my_list = [5 , 10 , 15 , 20]
2 total = sum ( my_list )
3 length = len ( my_list )
4 print ( total / length )

Output:

(c) (5 points)
1 my_dict = { ’a ’: 1 , ’b ’: 2}
2 val = my_dict . pop ( ’a ’)
3 my_dict [ ’c ’] = val + 2
4 print ( my_dict )

Output:

20
(d) (5 points)
1 is_raining = True
2 is_cold = False
3 if is_raining and is_cold :
4 print ( " Stay inside " )
5 elif not is_raining :
6 print ( " Go out " )
7 else :
8 print ( " Take an umbrella " )

Output:

21
Question 2: Control Flow Analysis (15 points)
Trace the following while loop. Show the value of ‘n‘ and ‘total‘ at the end of each
iteration, and write the final output.
1 n = 1
2 total = 0
3 while n < 10:
4 total += n
5 n += 2
6 print ( " Final total : " , total )

Trace Table and Final Output:

Question 3: Recursion Tracing (15 points)


Trace the execution of reverse("kiu"). Show each recursive call and what it returns
until the final result is built.
1 def reverse ( s ) :
2 if len ( s ) == 0:
3 return s
4 else :
5 return reverse ( s [1:]) + s [0]

Execution Trace for reverse("kiu"):

22
Question 4: Data Structures and Algorithms (25 points)
(a) (10 points)
Explain the ”divide and conquer” strategy used by the **binary search algorithm**.
Describe the roles of the ‘low‘, ‘high‘, and ‘mid‘ variables. What is the main prerequisite
for a list to be searchable with this algorithm?
Explanation:

(b) (15 points)


Trace the execution of the following binary search for ‘data = [2, 5, 8, 12, 16, 23, 38, 56]‘
and ‘target = 16‘. Show the values of ‘low‘, ‘high‘, and ‘mid‘ for each iteration.
1 def binary_search ( data , target ) :
2 low = 0
3 high = len ( data ) - 1
4

5 while low <= high :


6 mid = ( low + high ) // 2
7 print ( f " low : { low } , high : { high } , mid : { mid } " )
8

9 if data [ mid ] == target :


10 return True
11 elif data [ mid ] < target :
12 low = mid + 1
13 else :
14 high = mid - 1
15

16 return False
17

18 binary_search ([2 , 5 , 8 , 12 , 16 , 23 , 38 , 56] , 16)

Trace:

23
Question 5: Object-Oriented Programming Concepts
(25 points)
Consider the following Python classes:
1 class Father :
2 def skill ( self ) :
3 print ( " Gardening " )
4

5 class Mother :
6 def skill ( self ) :
7 print ( " Cooking " )
8

9 class Child ( Father , Mother ) :


10 def own_skill ( self ) :
11 print ( " Programming " )

(a) (10 points)


Answer the following conceptual questions:

1. This code shows multiple inheritance. What potential problem can arise from this,
often called the ”diamond problem” (though not fully present here)?

2. What is Method Resolution Order (MRO) and why is it important in multiple


inheritance?

3. According to Python’s MRO, which skill() method will be called on an object of


the Child class? Explain why.

4. How could you explicitly call the skill() method from the Mother class on a Child
object?

(b) (15 points)


Determine the output of the following code snippet.

24
1 c = Child ()
2 c . own_skill ()
3 c . skill ()
4

5 # Explicitly calling Mother ’s skill


6 Mother . skill ( c )

Output:

25

You might also like