CS2250 Test 1 Practice Questions

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/71

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 2:21 AM on 7/29/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

72 Terms

1
New cards

For the binarySearch function in Section 7.9.2, what is low and high after the first iteration of the while loop when invoking binarySearch([1, 4, 6, 8, 10, 15, 20], 11)?

low is 4 and high is 6

2
New cards

If a key is not in the list, the binarySearch function returns _________.

-(insertion point + 1)

3
New cards

If the binary search function returns -4, where should the key be inserted if you wish to insert the key into the list?

at index 3

4
New cards

Use the selectionSort function presented in this section to answer this question. Assume lst is [3.1, 3.1, 2.5, 6.4, 2.1], what is the content of list after the first iteration of the outer loop in the function?

2.1, 3.1, 2.5, 6.4, 3.1

5
New cards

Use the selectionSort function presented in this section to answer this question. What is list1 after executing the following statements? list1 = [3.1, 3.1, 2.5, 6.4] selectionSort(list1)

list1 is 2.5 3.1, 3.1, 6.4

6
New cards

__________ represents an entity in the real world that can be distinctly identified

An object

7
New cards

_______ is a template, blueprint, or contract that defines objects of the same type.

A class

8
New cards

The keyword __________ is required to define a class.

class

9
New cards

An object is an instance of a __________.

class

10
New cards

The constructor creates an object in the memory and invokes __________.

the __Innit__ method

11
New cards

Given the declaration x = Circle(), which of the following statements is most accurate.

x contains a reference to a Circle object.

12
New cards

Analyze the following code:

class A:
     def __init__(self, s):
         self.s = s
 
     def print(self):
         print(s)

 a = A("Welcome")
 a.print()

The program will be correct if you change print(s) to print(self.s).

13
New cards

Analyze the following code:

class A:
     def __init__(self, s = "Welcome"):
         self.s = s
 
     def print(self):
         print(self.s)

 a = A()
 a.print()

The program runs fine and prints Welcome

14
New cards

What will be displayed by the following code?

class Count:
    def __init__(self, count = 0):
        self.count = count

c1 = Count(2)
c2 = Count(2)
print(id(c1) == id(c2), end = " ")
print(c1.count == c2.count)

False True

15
New cards

Analyze the following code:

class MyDate:
    def __init__(self, year, month, day):
        self.year = year
        self.month = month
        self.day = day

class Name:
    def __init__(self, firstName, mi, lastName, birthDate):
        self.firstName = firstName
        self.mi = mi
        self.lastName = lastName
        self.birthDate = birthDate

birthDate = MyDate(1990, 1, 1)
name = Name("Ashley", 'F', "Weaver", birthDate)
birthDate = MyDate(1991, 1, 1)
birthDate.year = 1992
print(name.birthDate.year)

The program displays 1990.

16
New cards

What is the value of times displayed?

def main():
    myCount = Count()
    times = 0

    for i in range(0, 100):
        increment(myCount, times)

    print("myCount.count =", myCount.count, "times =", times)

def increment(c, times):
    c.count += 1
    times += 1

class Count:
    def __init__(self):
        self.count = 0

count is 100 times is 0

17
New cards

Analyze the following code:

    def __init__(self):
        self.x = 1
        self.__y = 1
 
    def getY(self):
        return self.__y

a = A()
print(a.x)

The program runs fine and prints 1

18
New cards

Analyze the following code:

class A:
    def __init__(self):
        self.x = 1
        self.__y = 1
 
    def getY(self):
        return self.__y

a = A()
print(a.__y)

The program has an error because y is private and cannot be accessed outside of the class.

19
New cards

How do you create a window?

window = Tk()

20
New cards

How do you create an event loop?

window.mainloop()

21
New cards

To create a label under parent window, use _______.

label = Label(window, text = "Welcome to Python")

22
New cards

To create a button under parent window with command processButton, use _______.

Button(window, text = "OK", command = processButton)

23
New cards

How do you create a frame?

frame = Frame()

24
New cards

Assume v1 = IntVar(), how do you set a new value 5 to v1.

v1.set(5)

25
New cards

Assume v1 = IntVar(), how do you create a radio button under parent frame1 with variable bound to v1 and value 1?

Radiobutton(frame1, text = "Yellow", bg = "yellow", variable = v1, value = 1, command = processRadiobutton)

26
New cards

How do you create a GUI component for displaying multiple-lines of text?

use Message

27
New cards

How do you create a text area?

use Text

28
New cards

How do you create a canvas under parent frame1 with background color white and foregroung color green?

Canvas(frame1, bg = "white", fg = "green")

29
New cards

How do you display a text "Good morning" centered at 30, 40 with color red?

canvas.create_text(30, 40, text = "Good morning", fill = "red")

30
New cards

To place a button in a specified row and column in its parent container, use ________.

grid manager

31
New cards

The side option of the pack manager may be  _____________.

LEFT, RIGHT, BOTTOM, TOP

32
New cards

Where is linear searching used?

When the list has only a few elements and when it is performing a single search in an unordered list

33
New cards

Which of the following is not a limitation of binary search algorithm?

Binary search algorithm is not efficient when the data elements more than 1500

34
New cards

Which of the following is a disadvantage of linear search?

Greater time complexities compared to other searching algorithms

35
New cards

For a binary search algorithm to work, it is necessary that the array (list) must be

sorted

36
New cards

How does selection sort work?

It repeatedly finds the minimum element and swaps it with the first unsorted element.

37
New cards

What is the main difference between binary search and linear search?

A. Binary search is more efficient than linear search.

B. Binary search requires more memory than linear search

C. Linear search works on unsorted arrays, while binary search does not.

D. Both A and B.

D. Both A and B.

38
New cards

Which of the following is a disadvantage of Selection Sort?

It has a time complexity of O(n^2), which is inefficient for large datasets.

39
New cards

What is the best case scenario for the Selection Sort algorithm?

Selection Sort performs the same regardless of the order of elements.

40
New cards

In Selection Sort, the element being selected is always placed in:

The correct position in the sorted portion.

41
New cards

Which part of the Selection Sort algorithm is responsible for finding the smallest element in the unsorted portion of the list?

The inner loop

42
New cards

How would you access an object's data field name using the dot operator?

object.name

43
New cards

In Python, which of the following refers to an object itself within its own method?

self

44
New cards

Which Python module provides the datetime class used for working with dates and times?

datetime

45
New cards

Which of the following is an example of encapsulation in Python

A.Making all data fields public

B.Using private data fields and methods to control access

C. Inheriting a class to extend functionality

D. Writing multiple functions for a class

B. Using private data fields and methods to control access

46
New cards

Which of the following objects is immutable in Python?

A. Tuple

B. List

C. Set

D. Dictionary

A. tuple

47
New cards

Why is it important to hide data fields in a Python class

To prevent unauthorized access and maintain data integrity

48
New cards

Analyze the following code:

 class A:
     def __init__(self, s):
         self.s = s
 
     def print(self):
         print(self.s)

 a = A()
 a.print()

The program has an error because the constructor is invoked without an argument.

49
New cards

In the following code, Which of the following is a private data field?

 def A:
 def __init__(self):
     __a = 1
     self.__b = 1
     self.__c__ = 1
     __d__ = 1

 # Other methods omitted

__b

50
New cards

Analyze the following code:

 class A:
     def init(self):
         self.x = 1
         self.__y = 1
 
     def getY(self):
         return self.__y

 a = A()
 a.__y = 45
 print(a.x())

The program has an error because y is private and cannot be accessed outside of the class.

51
New cards

Suppose i is 2 and j is 4, i + j is same as _________.

i.__add__(j)

52
New cards

To retrieve the character at index 3 from string s, use _________.

s.getitem(3) and s[3]

53
New cards

To return the length of string s, use _________. Please select all that apply.

s.len() and len(s)

54
New cards

How do you create a root object to access the Tkinter library  ?

root = tk.Tk()

55
New cards

How do you create a button?

btn = tk.Button(root, text = 'Click me !',)

56
New cards

To create a label under parent window, use _______.

name_label = tk.Label(root, text='Username')

57
New cards

To define an onclick event for a button, the attribute used is:

command

58
New cards

To get a value from textbox,  we use

name = name_entry.get()

59
New cards

Assume v1 =tk.BooleanVar() how do you create a check button with variable bound to v1 with root as the object to Tkinter library

tk.Checkbutton(root, text="2250, variable=v1)

60
New cards

How do you create a password field?

passw_label = tk.Label(root, text='Password')

61
New cards

How do you create a GUI component for displaying message?

Use messagebox

62
New cards

To perform an infinite loop for the window to display output of GUI taking root as the Tkinter object use:

root.mainloop()

63
New cards

checkbutton.pack(padx=40, pady=40) does what?

Places the checkbutton in the window

64
New cards

_______ are geometry managers in Tkinter. (write letters)

A. flow

B. grid

C. pack

D.place

B C D

65
New cards

The side option of the pack manager may be  _____________.

A. LEFT

B. RIGHT

C. BOTTOM

D. TOP

A B C D

66
New cards

To create an image, use ______________________.

image = PhotoImage(file = imagefilename)

67
New cards

You can create an image from a ____________ file.

A. .bmp

B. .png

C. .gif

D. .jpg

B and C

68
New cards

You can display an image in ______________.
A. an entry

B. a radio button

C. a check button

D. a button

E. a label

B C D E

69
New cards

How do you display a text "Good morning" centered at 30, 40 with color red?

canvas.create_text(30, 40, text = "Good morning", fill = "red")

70
New cards

How do you draw a polygon consisting of points (30, 40), (50, 50), (10, 100) filled with red color?

canvas.create_polygon(30, 40, 50, 50, 10, 100, fill = "red")

71
New cards

How do you draw a circle centered at 100, 100 with radius 100 on canvas?

canvas.create_oval(100 - 100, 100 - 100, 100 + 100, 100 + 100)

72
New cards

How do you draw a rectangle centered at 100, 100 with width 100 and height 100 on canvas?

canvas.create_rectangle(100 - 50, 100 - 50, 100 + 50, 100 + 50