CSE 101 Exam 1 Sample Questions
21 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 best describes Python's syntax?

  • The errors programmers often make in Python.
  • The guidelines and best practices for Python programming.
  • The comments typically written by Python programmers.
  • The rules programmers must follow when writing Python. (correct)

Which statement is FALSE regarding algorithms?

  • It requires finite resources.
  • It is a sequence of instructions.
  • It requires finite time.
  • It depends on a programming language. (correct)

Which of the following qualifies as a valid algorithm?

  • Counting from 0 to infinity by 5s.
  • Painting a picture of a flower.
  • The process for assembling a Tesla Model Y SUV. (correct)
  • A list of ingredients in a loaf of bread.

Which provides the BEST example of an algorithm?

<p>Directions for tying a necktie. (C)</p> Signup and view all the answers

What best describes the discipline of computer science?

<p>The study of problem-solving with the help of computers. (B)</p> Signup and view all the answers

Which of the following represents a valid algorithm?

<p>The procedure of counting from 0 to 10,000 by 5's. (D)</p> Signup and view all the answers

Which statement about the field of computer science is FALSE?

<p>It is primarily the study of computers. (B)</p> Signup and view all the answers

From a computer's perspective, what is a computer program?

<p>A list of instructions in machine-readable format. (B)</p> Signup and view all the answers

Which character sequence initiates a comment in Python?

<p>None of these. (B)</p> Signup and view all the answers

Given the assignment x = 3.14, what data type will the variable x have?

<p><code>float</code> (A)</p> Signup and view all the answers

What is the final value of x after executing x = 8 // 7 + 8 % 7?

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

What is the final value of x after executing x = 5 + 3 ** 2?

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

Consider x = 3 - 4 ** 2 / 5 + 7 * 8. Which operator is applied second during the expression evaluation?

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

What is the output of the following code: x = 10; x = "x" + "5"; print(x)?

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

What is the final value of x after the given Python code has finished executing? x = 4 ** 2

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

What will be the output of the following Python code? x = 5; y = 5; print(x + y)

<p>10 (B)</p> Signup and view all the answers

Given that n is a positive integer, under what condition does (n % 4) equal 1?

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

Which of the following is a valid variable name in Python?

<p>None of these. (B)</p> Signup and view all the answers

In Python, assuming cost stores a numerical value, which expression calculates 35% of the value stored?

<p><code>cost * 0.35</code> (B)</p> Signup and view all the answers

If miles stores the number of miles run and minutes stores the time taken in minutes, which code correctly calculates the average speed in miles per hour?

<p><code>speed = miles / (minutes / 60)</code> (D)</p> Signup and view all the answers

What is the final value of x after executing x = 11 // 2 in Python?

<p>5 (B)</p> Signup and view all the answers

Flashcards

What is Python's syntax?

The set of rules programmers must follow when writing code in Python.

What is an algorithm?

A sequence of instructions that requires finite time and resources.

What defines a valid algorithm?

A clearly defined sequence of steps to solve a problem, such as counting from 0 to 10,000 by 5's.

Example of an Algorithm?

Directions for tying a necktie.

Signup and view all the flashcards

What is Computer Science?

The study of problem-solving with computers, including software and hardware design.

Signup and view all the flashcards

Valid Algorithm?

The procedure of counting from 0 to 10,000 by 5's.

Signup and view all the flashcards

False about Computer Science?

The study of solving real world problems.

Signup and view all the flashcards

Computer Program?

A list of instructions in machine-readable format.

Signup and view all the flashcards

Python Comment?

"#"

Signup and view all the flashcards

Type of 3.14?

Float.

Signup and view all the flashcards

8 // 7 + 8 % 7

Signup and view all the flashcards

5 + 3 ** 2

Signup and view all the flashcards

3 - 4 ** 2 / 5 + 7 * 8

Signup and view all the flashcards

3 + 2 * 4

Signup and view all the flashcards

x = "x" + "5", print(x)

An error message.

Signup and view all the flashcards

x = 4 * 2

Signup and view all the flashcards

x = 12 // 2

Signup and view all the flashcards

x = 5, y = 5, print(x + y)

An error message.

Signup and view all the flashcards

When does (n % 4) equal 1?

Sometimes.

Signup and view all the flashcards

Valid Variable Name?

my_var.

Signup and view all the flashcards

Study Notes

  • This document provides sample questions for Exam #1 and appeared in the Fall 2024 offering of CSE 101.

Python Syntax

  • Python's syntax is the set of rules programmers must follow when writing programs in Python.

Algorithms

  • An Algorithm requires finite resources, finite time and is a sequence of instructions.
  • An algorithm is not considered correct if it depends on a programming language.
  • Examples of valid algorithms:
    • The process for assembling a Tesla Model Y SUV.
    • Directions for tying a necktie.
    • The procedure of counting from 0 to 10,000 by 5's.

Computer Science

  • Computer science is best summarized as studying problem-solving with computers.
  • Computer science involves computational thinking and applies to solving real-world problems.
  • Computer science is not primarily the study of computers.

Computer Programs

  • From a computer's perspective, a computer program is a list of instructions in machine-readable format.

Python Comments

  • In Python, comments start with '#'.

Python Variable Types

  • Assigning x = 3.14 will result in variable x of float type.

Python Operators

  • Considering "x = 3 - 4 ** 2 / 5 + 7 * 8", the exponentiation operator (**) is applied second.
  • Considering "x = 9 - 3 ** 2 / 3 + 5 * 6", the subtraction operator (-) will be applied last when the expression is evaluated.

Python Code Execution

  • The final value of x for x = 8 // 7 + 8 % 7 is 2.
  • The final value of x for x = 5 + 3 ** 2 is 14.
  • The final value of x for x = 3 + 2 * 4 after the given Python code has finished executing is 11.
  • The computed output for x = 4 - 2 is an error.
  • The final value of x for x = 12 // 2 after the given Python code has finished executing is 6.
  • "x = 2 % 10" will result in variable x be assigned the value 2.
  • The final value of x for x = 2 * (3 + 5) after the given Python code has finished executing is an error.
  • In the code x = 1; x += 4; x += 5, the final value of x is 10.
  • Given x = 11 / 2, the final value of x after the given Python code has finished executing is 5.5.
  • Given x = 1; x = x + 3, the final value of x after the given Python code has finished executing is 4.

Python Printing

  • The output of the code x = 10; x = "x" + "5"; print(x) is "x5".
  • The output of the code y = 5;print(x + y) and x=5 results in an error.

Calculations

  • To compute 35% of the value in the variable cost in Python, use cost * 0.35.
  • Given miles and minutes, speed = miles / (minutes / 60) correctly computes the average speed in miles per hour.
  • The value of 11 // 2 is 5.
  • The value of 11 % 2 is 1.

Variable operations

  • The type of the value stored in a variable determines the kinds of operations that can be applied to it in Python.
  • If a variable called gpa stores a value, the type of value that MUST be stored is a float.

Functions

  • The keyword used to call a function in Python is that no special keyword is needed.
  • A Python function can return two values at the same time with a single return statement.
  • To get "The answer is 10" with the code provided, insert return 2*x in the blank line.
  • Import statements are most frequently included inside a function definition.
  • Calling a function without knowing how it was written is an application of abstraction.
  • The statements inside a Python function's body must be indented.
  • The keyword typically found on the last line of a Python function is return.
  • The Python interpreter knows whether to perform addition or concatenation of v1 + v2 depending on the values stored in v1 and v2.
  • If a Colab cell contains only def compute(x):\n return 'Hello', no output will be produced when this code cell is run.

Conditional Execution

  • Conditional execution refers to running specific code only when a certain condition is true.

Python Code Output

  • For the code a = 4; if a >= 4: print('W'); if a != 4: print('X'); else: print('Y'); print('Z'), the output is W, Y and Z only.
  • The following expressions will always evaluate to True for any value of x and y:
    • x == y or y == x
    • x == y and x != y
  • For a = 9; if a <= 9: print(’W’); else if a >= 3: print('X'); else: print('Y'); print('Z’), the output is W only.
  • For the code: a = 9; if a <= 9: print('W'); elif a >= 3: print('X'); if a <= 4: print('Y'); print('Z'); the output is W, X and Z only.
  • A function named mixup most likely returns a Boolean value.
  • For x % 5 != 0 will evaluate to true for any positive integer x that is NOT divisible by 5.
  • Given a = 3 if a > 3: print('E') elif a > 5: print('F') else: print('G'), the output is G only.
  • Given a = 1 if a >= 2: print('E') elif a == 1: print('F') else: print('G') print('H'), the output is F and H only.
  • Given a = 4 if a = 7: print('W') else: print('X') print('Y'), the output is X only.
  • Given a = 2 if a >= 2: print('E') elif a == 2: print('F') else: print('G') print('H'), the output is E and H only.
  • Given a = True b = True print(a or b), the output is True.
  • Given a = 3 if a >= 2: print('W’) elif a >= 2: print('X') else: print('Y') print('Z'), the output is W only.
  • Given a = True b = False print(a and b), the output is False.
  • Given a = 9 if a <= 9: print('W') elif a >= 3: print('X') if a <= 4: print('Y') print('Z'), the output is W, X and Z only.

String operations

  • Given term = 'software' print(term[2:]), the output is ftware.
  • An index of the letter h in the string ‘python’ is 3.
  • Given cheer = 'Stony!' * 3 print(cheer), the output is Stony!Stony!Stony!.
  • String concatenation means to join two strings together.
  • Given word = 'SCHOOL' i = word.find('Z') print(word[i:]), the output is no output.
  • Given term = 'software' term[1] = 'X' print(term), results in an Error Message.
  • Given term stores a string, len(term)-1 gives the index of the rightmost character of the string stored in term?
  • Given word = 'program' x = word.find('h’) print(x), will output -1.
  • Given word = 'SOFTware' word.upper() print(word), will output SOFTware.
  • Given term = 'software' print(term[-3]), the output is a.
  • Given term = 'software' print(term[:2]), the output is so.
  • Valid indices of the characters in the string ‘software’ is 0 through 7, inclusive.
  • Given word = 'software' print(word[2:4]), the output is ft.
  • Given s = 'Python' print(s[-1]), the output is n.
  • The correct way to reverse a string s is s[::-1].
  • In a function extract_vowels(s), vowels.append(char) correctly completes the function.
  • Given s = 'Hello, World!' print(s[7:]), the output is ,_World!.
  • A line of code that would correctly complete the function to shift each lowercase letter in text two positions forward in the alphabet, wrapping around from 'a' to 'z' is: chr(((ord(char) - ord('a') + 2) % 26) + ord('a')).
  • The output for s = 'abcdef' for i in range(1, len(s), 2): print(s[i]) is bdf.
  • The len () function returns the number of characters when used on a string.
  • The method used to convert a string to lowercase is s.lower().
  • The code print(chr(ord('a') + 1)) will output 'b'.
  • For iterating over a string s, both styles of "for" loops are equivalent in that they iterate over each character in the string s in the order specified.
  • Given a function reverse_words(sentence), the line(s) that correctly completes the function are: \n words = sentence.split()\n rev_sentence =" ".join(words[::-1]) .
  • "s.slice()" is not a valid string method.
  • To check if a substring sub exists in a string s. if sub in s:

Loops

  • "for i in range(1, 11): print(i)" would print numbers from 1 to 10, inclusive.
  • Given s = 'abcde' print(s[1:4]), the output is bcd.
  • Given s = 'heLLo' print(s.upper()), the output is HELLO.
  • The valid way to concatenate two strings is str1 + str2.

Studying That Suits You

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

Quiz Team

Related Documents

Description

Sample questions from the Fall 2024 offering of CSE 101. Topics include Python syntax, algorithms, computer science, and computer programs. Also covers what an algorithm requires, and how computer science applies to solving real-world problems, as well as Python comments.

More Like This

Use Quizgecko on...
Browser
Browser