CMPT 141.3 Practice Exam

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which of the following is an example of an algorithm?

  • A box of parts for building a desk
  • A recipe for baking a cake (correct)
  • A movie theater schedule listing showtimes
  • A voting ballot listing candidates and their political parties

What is the value of p after the given initializations?

  • 10.0
  • 11.5
  • 11.0 (correct)
  • 13.0

In total, how many function calls can be found in this program?

  • 4
  • 2 (correct)
  • 1
  • 3

What does the function 'max()' return in the program?

<p>The length of the longer name string (A)</p> Signup and view all the answers

What type of value does the function 'float()' attempt to produce?

<p>Floating-point number (C)</p> Signup and view all the answers

Which option correctly checks if the string variable s represents a digit?

<p>C.s.isdigit() (A)</p> Signup and view all the answers

What is the correct way to import the math library using an alias m?

<p>import math as m (A)</p> Signup and view all the answers

What will the value of the expression s[-1:-len(s)-1:-1] be when s is 'DetectivePikachu'?

<p>'uhcakiPevitceteD' (A)</p> Signup and view all the answers

Evaluate the expression s[1:len(s):4] for s = 'DetectivePikachu'. What is returned?

<p>'etPc' (D)</p> Signup and view all the answers

Which expression will evaluate to TRUE given x = 4 and y = 77?

<p>x &gt; y or x &gt; 0 (D)</p> Signup and view all the answers

Which of the following expressions evaluates to TRUE?

<p>not (x == y and x &gt; 10 or y &gt; 10) (C)</p> Signup and view all the answers

What will the console output be after running the given program regarding speed and turbo?

<p>50 (C)</p> Signup and view all the answers

What will the program print when ptype is 'grass' and level is 3?

<p>unknown (D)</p> Signup and view all the answers

Which version of the formatTime() function would display 1:35 when called with print(formatTime(95))?

<p>Version 1 (C)</p> Signup and view all the answers

What is the correct function definition to print 'Hello, Pikachu!'?

<p>def greet(s): (C)</p> Signup and view all the answers

Which line of code correctly fills in the BLANK to print 1234 to the console?

<p>answer = combine_digits(12, 34) (B)</p> Signup and view all the answers

Given the definition of isdigit(), what should replace BLANK to convert string s to an integer if s contains only digits?

<p>int(s) (C)</p> Signup and view all the answers

Which version of the formatTime() function causes an error due to a typo?

<p>Version 2 (C)</p> Signup and view all the answers

What is the outcome of calling formatTime() with a negative totalMinutes value?

<p>It returns a formatted string with negative hours and minutes. (D)</p> Signup and view all the answers

Which function version does not return any value when checking print(formatTime(95))?

<p>Version 3 (D)</p> Signup and view all the answers

Which version of the greet function correctly captures the argument passed?

<p>def greet(s): (D)</p> Signup and view all the answers

Signup and view all the answers

Flashcards

Algorithm Example

An algorithm is a set of instructions for solving a problem. A recipe for baking a cake follows a specific sequence of steps to achieve the desired outcome.

Variable Initialization

Assigning an initial value to a variable before using it in calculations.

Integer Division (//)

Dividing two integers and discarding the remainder.

Function Call (max)

Invoking a built-in function to perform a specific task, like finding the maximum value.

Signup and view all the flashcards

Function Call (len)

Calling a function to determine the length of a string (number of characters).

Signup and view all the flashcards

String Concatenation

Combining two strings into a single string using the plus operator (+).

Signup and view all the flashcards

Type Casting (float)

Converting a value from one data type to another (here, integer to floating-point).

Signup and view all the flashcards

Console Output (print)

Displaying text or values on the console (screen) using the print function.

Signup and view all the flashcards

formatTime() (Version 1)

This version calculates hours and minutes correctly and returns the formatted time as a string.

Signup and view all the flashcards

formatTime() (Version 2)

This version calculates hours and minutes, but converts them to strings and prints the formatted time.

Signup and view all the flashcards

formatTime() (Version 3)

This version takes user input for total minutes but doesn't use it correctly, causing an error.

Signup and view all the flashcards

formatTime() (Version 4)

This version calculates hours and minutes, converts to strings, and then prints the formatted time using 'print()'.

Signup and view all the flashcards

greet() Function

The greet() function takes a string argument, concatenates it to the greeting, and prints the final message.

Signup and view all the flashcards

combine_digits() Function

The combine_digits() function takes two integers, converts them to strings, concatenates them, and returns the combined integer as a result.

Signup and view all the flashcards

isdigit() Method

The isdigit() method is a string object method that checks if a string consists only of digits.

Signup and view all the flashcards

Converting to Integer

To convert a string to an integer, the 'int()' function is used if the string consists only of digits.

Signup and view all the flashcards

isdigit()

The isdigit() method checks if a string contains only digits (0-9). It returns True if it does, otherwise False.

Signup and view all the flashcards

Importing a Library

Importing a library allows you to use functions and classes defined in that library in your Python code.

Signup and view all the flashcards

String Slicing with Negative Indices

Negative indices in Python string slicing count from the end of the string. -1 refers to the last character, -2 to the second-to-last, and so on.

Signup and view all the flashcards

String Slicing with Step

The third parameter in Python string slicing specifies the step. A step of 4 means that the string is sliced taking every fourth character.

Signup and view all the flashcards

Logical Operators (and, or, not)

Logical operators combine boolean expressions to create more complex conditions. 'and' returns True only if both expressions are True. 'or' returns True if at least one expression is True. 'not' inverts the truth value.

Signup and view all the flashcards

if-elif-else Statements

if-elif-else statements allow you to execute different blocks of code based on different conditions. Code in the first 'if' block is executed if its condition is True. If the first condition is False, the 'elif' blocks are checked in order. If none of the conditions are True, the 'else' block is executed.

Signup and view all the flashcards

Variable Scope

The scope of a variable determines where in the code it can be accessed. Variables declared inside a function have a local scope, meaning they can only be accessed within that function. Variables declared outside of any function have a global scope, meaning they can be accessed from anywhere in the code.

Signup and view all the flashcards

Conditional Execution

Conditional execution is a way to control the flow of a program based on different conditions. 'if' statements check a condition and execute different code blocks depending on whether the condition is True or False.

Signup and view all the flashcards

Study Notes

Exam Information

  • Marks: 75
  • Time: 180 minutes
  • Exam type: Practice final examination
  • Course: CMPT 141.3, Department of Computer Science, University of Saskatchewan

Part I - Multiple Choice

  • Question 1: Algorithm example.

    • Answer: A recipe for baking a cake.
  • Question 2: Value of p.

    • Variables: x=3.3, y=2.2, z=1.1, a=3, b=2, c=5, p=1.5+c*b
    • Answer: C. 11.5
  • Question 3: Value of q.

    • Variables: x=3.3, y=2.2, z=1.1, a=3, b=2, c=5, q=x-a/c+z
    • Answer: D. 3.4
  • Question 4: Value of r.

    • Variables: x=3.3, y=2.2, z=1.1, a=3, b=2, c=5, r=x+a*b/c
    • Answer: E. 1.42
  • Question 5: Function calls count.

    • Answer: 2
  • Question 6: Console output

    • Variables: a = 7
    • Answer: D. 7.0
  • Question 7: Version of the function formatTime.

    • Input: formatTime(95)
    • Answer: D. Version 4
  • Question 8: Line of code to print "Hello, Pikachu!"

    • Code: message = "Hello," + s + "!"
    • Answer: E. def greet(s):
  • Question 9: Line of code to print 1234.

    • Answer: C. answer = combine_digits(12, 34)
  • Question 10: Code to convert the string s to an integer.

    • Answer: C. s.isdigit()

Part II - Written Answers

  • Question 47: Frostbite warning determination.

    • The function should:
    • Accepts temperature and windspeed
    • Return True if a frostbite warning is needed; false otherwise.
    • If temp is less than -40 or (temp is less than -25 and windspeed is greater than or equal to 20), issue the warning.
  • Question 48: Sum of even numbers up to 1000.

    • The program should compute the sum of all even numbers from 1 to 1000 (inclusive).
    • Answer: Calculate the sum of even numbers using a loop.
  • Question 49: Two-letter permutations of the string "ABCDEFG".

    • The code should output all possible combinations of two letters from the string "ABCDEFG", maintaining order matters when generating different two-letter combinations.
  • Question 50: Adding "Ash" to the end of classlist.

    • Answer: classlist.append("Ash")
  • Question 51: Names from classlist starting with "A".

    • Answer: [name for name in classlist if name[0] == "A"]
  • Question 52: Student names with grades of 80 or higher.

    • The program should iterate through the student grades (from the dictionary classgrades) and print the student names if their grade is 80 or greater.
  • Question 53: Word count in a text file.

    • Answer: This program reads a file, splits each line into words, counts the number of words in the file, and prints out the total word count.

Other questions (number 54, 55, 56, 57)

  • These questions involve writing recursive functions and solving problems based on algorithm analysis and implementations
  • The responses will include pseudocode
  • The exam involves a lot of conceptual and technical details for understanding program implementation

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser