Exam 1 Practice Problems in Programming
19 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the output of the following code? a = 5; b = 'b'; c = True; print("The answer is...", end=' '); if a != 10: print("A", end=' "); elif b == 'b': print("B", end=' "); else: print("C", end=' "); z = c and bool(a); print(z, end=' '); d = a ** 3 + 25 % 3 - 12 // 5; print(d)

The answer is... A True 131

What is the output of the following code? n = 1; p = 'A'; while n < 10: p += p; n += 3; print(n, p)

10 AAAAAAAA

What will be the output of the following code? a = True; b = bool('False'); c = 5 > 8; d = a and b and c; e = not a or not (b and c); print(d, e)

False True

What is the output of the following code? mystrs = ['Good Bull', 'Whoop', 'Hullabaloo', 'Howdy', "Gig 'em", 'Aggies']; mynums = [3, 5, 4, 1, 2]; for num in mynums: print(mystrs[num], end=' ')

<p>Howdy Aggies Hullabaloo Whoop</p> Signup and view all the answers

What will be the output of the following code? mylist = []; for i in range(5): mylist.append(i ** 2); print(mylist[-3:])

<p>[1, 4, 9]</p> Signup and view all the answers

What is the output of the following code? x = 5 / 5; print(x)

<p>1.0</p> Signup and view all the answers

What will be the output of the following code? mystr = 'The quick brown fox jumped over the lazy dog'; print(mystr[:3], end=' '); if mystr == 'q': ...

<p>The down</p> Signup and view all the answers

What is the output of this code? mystr = "Howdy!Welcome to Texas A&M Engineering!"; print(mystr[:5] + mystr + mystr[-22:-1] + ' students!')

<p>Howdy!Welcome to Texas A&amp;M Engineering!go Engineering! students!</p> Signup and view all the answers

What is the output of the following code? x = 4; y = "Gig'em Aggies!"; while x < 100: print(x, y); x *= x - 2; y += y

<p>4 Gig'em Aggies!</p> Signup and view all the answers

What will be the output of this code? x = 5; sum = 0; for i in range(4): x *= i; sum += x; print(x, sum)

<p>0 0</p> Signup and view all the answers

What will be the result of executing this code? AB = 0; V = [9, 5, -3, 6, -1, 0]; for i in range(len(V) - 2): if V[i] < 0: AB += 1; print("AB =", AB)

<p>AB = 2</p> Signup and view all the answers

What would be the output of this code? x = 4; y = 8; t = x; x = y; y = t; z = x / y; print(z)

<p>1.0</p> Signup and view all the answers

What is the output of this code? x = 5 % 2 == 1 and 5 < 2 + 4; print(x)

<p>True</p> Signup and view all the answers

What will be printed by this code? a = 1; b = 2; c = "a"; d = int(float("3.14")); if a == 1 and d == 3.14: print("Green"); elif c == a or d > 3: print("Red"); else: print("Yellow")

<p>Red</p> Signup and view all the answers

What is the expected output? x = 10; y = 5; if x % 2 == 0: if y > 5: print("A"); else: print("B"); print("C"); else: if y < 5: print("D"); else: print("E"); print("F"); print("G")

<p>B C G</p> Signup and view all the answers

What is the result of running this code? x = 2; y = "A"; while x < 100: print(x, y); x *= x; y += y

<p>2 A 4 AA 16 AAAA 256 AAAAAAAA</p> Signup and view all the answers

What will be printed by this code? for i in range(12): if i % 2 != 1 and i % 3 == 0: print(i)

<p>0 6</p> Signup and view all the answers

What is the output of the following code? data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]; print(data)

<p>[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]</p> Signup and view all the answers

The variable 'numSum' is defined as the sum of two integers, num1 and num2. The number of digits in numSum can be found by which code snippet? num1 = int(input("Enter first integer: ").

<p>numDigits = len(str(abs(numSum)))</p> Signup and view all the answers

Study Notes

Exam 1 Practice Problems

  • Students are advised to practice the problems using pen and paper, not an IDE.
  • Code solutions should be commented, and algorithms should be outlined before writing code.
  • Quizzes should be reviewed for additional autograded style problems, including fill in the blank, multiple choice, multiple answer, and true/false questions.
  • Partial credit will be available for code writing problems, but not for autograded problems.
  • Students may not use calculators, phones, the internet, laptops, books, notes, lectures on Canvas, or any other form of electronic media during the exam.
  • The following problems should be attempted using pencil and paper, and solutions should be checked in an IDE afterwards.

Problem 1

  • The output: The answer is... A True 114
  • The if-elif-else statement checks the values of a and b and prints accordingly.
  • The variable z is assigned the result of c and bool(a), which evaluates to True and is printed.
  • The variable d calculates the result of a ** 3 + 25 % 3 - 12 // 5 and prints the result, which is 114.

Problem 2

  • The output: 10 AAA
  • The while loop iterates until n is less than 10.
  • Each iteration, the variable p is doubled and n is increased by 3.
  • The final values of n and p are printed.

Problem 3

  • The output: False True
  • The variable a is assigned True, and b is assigned True because bool('False') evaluates to True.
  • The variable c is assigned False as 5 > 8 is false.
  • The variable d is assigned False because the and operator requires all conditions to be true.
  • The variable e is assigned True because the or operator evaluates to True if at least one condition is True.

Problem 4

  • The output: Howdy Gig 'em Aggies Whoop Hullabaloo Good Bull
  • The for loop iterates through the elements of the mynums list.
  • For each element, the code prints the corresponding string element from the mystrs list.

Problem 5

  • The output: [9, 16, 25]
  • The for loop iterates 5 times, appending the square of i to mylist.
  • The function mylist[-3:] returns the last 3 elements of mylist.

Problem 6

  • The output: 1.0
  • The variable x is assigned the result of 5/5, which is 1.0.
  • The code prints the value of x.

Problem 7

  • The output: The fox brown cow sat down
  • The if-elif-else statement checks conditions based on the content of mystr.
  • The if branch executes if the string mystr is equal to "q", and prints fox and jumped.
  • The elif branch executes if the string mystr contains “x”, and prints brown cow and sat.
  • The else branch executes if the previous conditions are False, and prints quick brown fox jumped over the lazy.

Problem 8

  • The output: Howdy!Howdy!Welcome to Texas A&M Engineering! Engineering! students!
  • The code concatenates multiple substrings of mystr and prints the result.

Problem 9

  • The output:
    4 Gig'em Aggies!
    4 Gig'em Aggies!Gig'em Aggies!
    
  • The while loop iterates until x is less than 100.
  • Each iteration, the code prints x and y, then multiplies x by itself minus 2, and concatenates y with itself.
  • The loop terminates after two iterations because the value of x becomes less than 100.

Problem 10

  • The output: 0 0
  • The for loop iterates 4 times.
  • Each iteration, the code multiplies x by i, and adds the result to sum.
  • The code prints the final value of x and sum.

Problem 11

  • The output: AB = 2
  • The for loop iterates through the elements of V list, excluding the last two.
  • It counts the number of negative elements and updates AB accordingly.
  • The final value of AB is printed.

Problem 12

  • The output: 2.0
  • The code swaps the values of x and y using a temporary variable t.
  • It then calculates z by dividing x by y and prints the result.

Problem 13

  • The output: True
  • The code evaluates the expression 5 % 2 == 1 and 5 < 2 + 4 which is True and True, ultimately evaluating to True.

Problem 14

  • The output: Green
  • The if statement checks if a is equal to 1 and d is equal to 3.14, prints "Green" because both are true.

Problem 15

  • The output:
    B
    C
    G
    
  • The if-else statement checks the values of x and y and prints the corresponding result.

Problem 16

  • The output:
    2 A
    4 AA
    
  • The while loop iterates until x is less than 100.
  • Each iteration the code prints x and y, then squares x and concatenates y with itself.
  • The loop terminates after two iterations because the value of x becomes greater than 100.

Problem 17

  • The output:
     0
     6
     12
    
  • The for loop iterates from 0 to 11.
  • The if statement checks if i is not divisible by 2 and divisible by 3.
  • If true, the corresponding i value is printed.

Problem 18

  • The output:
    [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
    
  • The code prints the nested list data, showing the structure of nested lists.

Problem 19

  • The code snippet to find the number of digits of the sum of two integers:
    myStr = str(numSum)
    print(len(myStr)) 
    
  • The code converts the numSum integer to a string (myStr).
  • It then uses the len function to determine the length of the string, which represents the number of digits.
  • The len function tells us the length of the string, which corresponds to the number of digits in the sum.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Description

Prepare for your Exam 1 with these practice problems focusing on algorithms and code writing. Students should solve the problems on pen and paper and review their solutions in an IDE. Ensure thorough understanding of conditional statements and boolean logic.

More Like This

Use Quizgecko on...
Browser
Browser