IT101-2 Finals Flashcards
Who created python?
Guido Van Rossum
When was python created?
1991
What is python used for?
web development (server-side),
software development,
mathematics,
system scripting.
Whenever you are done in the python command line, you can simply type __________ to quit the python command line.
exit()
*indicate a block of code.
Indentations
Comments start with a ______, and Python will render the rest of the line as a comment
#
A _________ is created the moment you first assign a value to it.
variable
Rules of variables
- Must start with letter or underscore
-can’t start with number
-can only contain alpha numeric and underscore
There are three numeric types in Python:
*int
*float
*complex
is a named place in the memory where a programmer can store data and later retrieve the data using the variable “nameâ€
variable
You can change the contents of a variable in a later statement
T or F
T
We assign a value to a variable using the ________?
Assignment statement (=)
Order of operation for python
Pemdas
Parenthesis
Exponent
multiplication, division, and remainder
Addition subtraction
(from left to right)
When writing code - use __________
parenthesis
You can also use str() and float() to convert between strings and integers
True or False
False
int() and float()
you will not get an error if the string does not contain numeric characters while converting to int or float
T or F
F
You will get an Error
used to output variables
print()
execution of code statements (one line after another) – like following a recipe
Sequential
used for decisions, branching – choosing between 2 or more alternative paths.
Selection
used for looping
repetition
Recall that Python has a type called __________, which can take two possible values, True and False
boolean
An expression that evaluates to a true/false value is called a
Boolean expression
Python, decisions are made with the ___________, also known as the __________________.
if statement
selection statement
If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if statement is executed. If boolean expression evaluates to FALSE, then the first set of code after the end of the if statement(s) is executed
T or F
T
can be followed by an optional else statement, which executes when the boolean expression is FALSE.
An if statement
contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value.
else statement
allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.
The elif statement
allows us to execute a statement or group of statements multiple times
loop statement
Repeats a statement or group of statements while a given condition is TRUE
while loop
The loop iterates while the condition is false.
When the condition becomes true, program control passes to the line immediately following the loop.
T or F
F
Loop iterates when condition is true
A loop becomes ____________ if a condition never becomes FALSE.
infinite loop
If the _________ statement is used with a for loop, the ____ statement is executed when the condition becomes false.
else
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
For loop
is a block of organized, reusable code that is used to perform a single, related action.
A function
The code block within every function starts with a ___________ and is ___________
colon (:) and is indented.
The statement __________ exits a function, optionally passing back an expression to the caller
return [expression]
A return statement with no argument returns an error
False
It returns as None
______________ can be accessed only inside the function in which they are declared,
local variables
__________ can be accessed throughout the program body by all functions.
global variables
is a Python object with arbitrarily named attributes that you can bind and reference.
A module
You can use any Python source file as a module by executing an _______________ in some other Python source file.
import statement
The ________ built-in function returns a sorted list of strings containing the names defined by a module.
dir()
powerful and flexible data structure in python that allows you to store and manipulate collections of elements
Lists
Python lists can grow or shrink in size as needed making them flexinle for handling varying amounts of data
Dynamic storage and retrieval
Lists are commonly used in _____ to iterate over the elements and perform operations on them
loops
to create a python list you need to use ____
[] square brackets
List elements are accessed using their ________
indices
the indices start from ____ for the first elements
Lists can store elements of any data types
T or F
t
This list function return the number of elements present in a list
len()
allows you to add an element at the end of the list
append()
adds elements from another list to the end of the current list.
extend()
allows you to insert an element at a specific index in the list
insert()
function is used to remove the first occurrence of a specified element from the list
remove()
used to remove and return the element at a specific index in a list
pop()
removes all elements from the list
clear()
returns the index of the first occurrence of a specified element in the list
index()
returns the number of occurrences a specific element in the list
count()
used to sort in the list ascending order.
sort()
reverses the order of elements in the list
reverse()
creates a shallow copy of the list
copy()
provide a powerful set of tools for manipulating and managing lists
list functions
a concise and pythonic way of generating lists
List comprehension
List comprehension does not allow it to be nested
T or F
F
it is allowed to be nested to allow complex lists
is used to apply the int() function to each element to in the input list
map()
used to include only certain elements
filter()
this is generally faster and more efficient compared to traditional loops when dealing with simple operations on lists
List comprehension
an ORDERED collection of elements enclosed in parentheses ()
Tuples
Tuples are immutable
True or false
t
Elements cannot be modified once they are defined
Immutable
tuples are typically created by enclosing elements within ____ and separating them with _________
parentheses ()
Comma ,
Individual elements within a tuple can be accessed using __________
indexing
involves creating a tuple by assigning values to it
tuple packing
allows you to assign individual elements of a tuple to separate variables
tuple unpacking
Tuples can be concatenated using the “*” operator?
T or F
False you use “+”
Tuples can be repreated using the “x” operator
T or F
False you use the “*”
to check if an element exists in a tuple use the _____ keyword
in
tuples are _______, meaning their element cannot be changed after creation
immutable
you can convert lists and vice versa using the list() and tuple() function
T or F
t
Functions can only return single values as a tuple
T or F
False it can return multiple values
is an immutable unordered collection of UNIQUE elements
set
Sets are widely used for performing ___________ such as union, intersection and difference
mathematical set operations
sets can be created using _________
curly braces {}
You can use this function to add a single element in a set
add()
you can use this fucntion to add multiple elements in a set
update()
removes specified element from the set if it does not exist it will raise an error
remove()
this removes an element and if it does not exist it will not raise an error
discard()
removes a random item from a set
pop()
This set operations combines returns a combination of two sets
Union
symbol of union
|
This set operation return what’s common between two sets
Intersection
Symbol of intersection
&
returns the difference between 2 sets or more
difference
symbol of difference
-
returns what is unique in both sets
Symmetric difference
symbol of symmetric difference
\^
to test if a set is a subset use
issubset()
to test if a set is a superset use
issuperset()
Sets only store unique elements, you can use them to store duplicates.
T or F
t
Allows you to store and retrieve data in key-value pairs
Dictionary
Another name of dictionary
Associative array or hash table
T or F
Dictionary is an ordered collection
F it is unordered
Dictionary key must be ______ and ______
unique and immutable
Dictionaries are created using
{} curly braces
To retrieve the value associated with a specific key use the _________
square bracket [] with the key inside
If you try to access a key that does not exist it will raise an index error
T or F
F it will raise a KeyError
You can use the ______ method which returns a default value if they key is not found
get()
to remove an element from the dictionary you can use the
pop() or del Keyword
to copy a dictionary use
copy() or dict() constrictor
the most common approach is to loop through its keys
T or F
T
How is dict separated
it is separated by colon (:), and the elements are separated by commas (,)
Question
Answer
a sequence of characters
string
computers do not deal with characters they deal with___________
numbers (binary)
Conversion of character to number is called ______ and the reverse is called ___________
encoding
decoding
___________ and _________ are some of the popular encodings used.
ASCII and Unicode
In python a string is a sequence of _________ characters
unicode
was introduced to include every character in all languages and bring uniformity in encoding
Unicode
You can access individual characters using
indexing
Trying to access a char out of index range will raise an
indexerror
An index must be an integer any other type will result in
TypeError
Strings are Immutable
T or F
T
To delete tge string entirely is possible using the
del keyword
returns an enumerate object, it contains the index and value of all the items
enumerate()
starts with a backslash and is interpreted differently
escape sequence
To ignore the escape sequence inside a string place a
r or R infront of the string
is available with the string object is very versatile and powerful for FORMATTING strings
format()
format strings contain ___________ as place holders
curly braces{}
Format are separated from the field name using
Colon
is a module to manipulate and manage date and time information
datetime
the handling for date time is
datetime.datetime
you need to import datetime before ytou use it
T or F
T
To format datetime objects use
strftime()
to perfrom arithmetic operations in datetime use
timedelta
to work with timezones in datetime objects use
pytz library
are named locations on disk to store related information
Files
store the data in the form of characters.
Text files
store entire data in the form of bytes.
Binary files
are generally available in .jpg, .gif or .png formats.
Image files
CRUD meaning
Creat read update delete
we use _________ function to open a file
open()
represents a temporary block of memory
buffer
What does w open mode does?
Write data into file
What does “r” does
Read data from the file
What does a does?
append data to the file?
What does w+ do?
to write and read data of a file
What does r+ do?
To read and write data into file
What does a+ do?
To append and read data of file.
What does x do?
open file in exclusing creation mode.
To close a file use __________
close()
closing is mandatory
T or F
t
When a file is not closed a problem will lead to
insufficient memory.
to create a fille use
open() method
to create a new file which parameters do you need?
x
a
W
TO read data from text file use
read()
the____________ method returns a list of remaining lines of the entire file
readlines()
To delete you much import
the os module and run the os.remove() function
to know the current directory
os.getcwd()
to make a sub directory
os.mkdir(‘mysub’)
to change directory
os.chdir()
to rmeove directory
os.rmdir()
to rename directory
os.rename()
what is the error if not found using os.chdir()
FileNotFoundError
What happens when you delete a directory that has something inside of it
OSERROR
What does csv mean?
Comma separated Value
Do you need to import a csv file to use it?
T or F
T
how to read in csv
csv.reader()
How to write in csv
csv.writer()
how to write a row of data in csv
writerow()
disrupts the flow of instructions
exception
this is optional and is used for code that needs to be executed regardless of whether an exception occurred or not
Finally
you can raise exceptions using the
raise statement
What does function overloading mean (in python)?
The assignment of more than one behavior to a particular function. The operation performed varies by the types of objects or arguments involved.
An individual object of a certain class. An object obj that belongs to a class Circle, for example, is an instance of the class Circle.
the function overloads and kills the computer
A user-defined prototype for an object that defines a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.
The assignment of more than one behavior to a particular function. The operation performed varies by the types of objects or arguments involved.
Which of these is not a fundamental features of OOP?
Encapsulation
Instantiation
Inheritance
Polymorphism
Instantiation
Which of the following best describes inheritance?
Means of bundling instance variables and methods in order to restrict access to certain class members
Focuses on variables and passing of variables to functions
Allows for implementation of elegant software that is well designed and easily modified
Ability of a class to derive members of another class as a part of its own definition
Ability of a class to derive members of another class as a part of its own definition
Which of the following is the most suitable definition for encapsulation?
Focuses on variables and passing of variables to functions
Means of bundling instance variables and methods in order to restrict access to certain class members
Allows for implementation of elegant software that is well designed and easily modified
Ability of a class to derive members of another class as a part of its own definition
Means of bundling instance variables and methods in order to restrict access to certain class members
Which of the following is required to create a new instance of the class?
A value-returning method
A constructor
A None method
A class
A constructor
Which of the following statements is most accurate for the declaration x = Circle()?
x contains an object of the Circle type.
x contains an int value.
x contains a reference to a Circle object.
You can assign an int value to x.
x contains a reference to a Circle object.
Which of the following best describes polymorphism?
Means of bundling instance variables and methods in order to restrict access to certain class members
Focuses on variables and passing of variables to functions
Ability of a class to derive members of another class as a part of its own definition
Allows for objects of different types and behaviour to be treated as the same general type
Allows for objects of different types and behaviour to be treated as the same general type
Which of the following represents a template, blueprint, or contract that defines objects of the same type?
A method
A class
An object
A data field
A class
Which of the following best describes inheritance?
Focuses on variables and passing of variables to functions.
Allows for implementation of elegant software that is well designed and easily modified.
Means of bundling instance variables and methods in order to restrict access to certain class members.
Ability of a class to derive members of another class as a part of its own definition.
Ability of a class to derive members of another class as a part of its own definition.
Suppose B is a subclass of A, to invoke the __init__ method in A from B, what is the line of code you should write?
B.__init__(self)
A.__init__(self)
B.__init__(A)
A.__init__(B)
Q
A.__init__(self)
What is Instantiation in terms of OOP terminology?
Modifying an instance of class
Deleting an instance of class
Creating an instance of class
Copying an instance of class
Creating an instance of class
Which of the following statements are correct?
An object can contain the references to other objects.
A reference variable refers to an object.
An object may contain other objects.
A reference variable is an object.
An object can contain the references to other objects.
Correct: A reference variable refers to an object.
What is an instance in python?
A running program.
The creation of an instance of a class.
A special kind of function that is defined in a class definition.
An individual object of a certain class.
An individual object of a certain class.
What is the purpose of a constructor in Python?
To declare variables.
To handle exceptions.
To initialize objects when they are created.
To define methods.
To initialize objects when they are created.
In Python, polymorphism enables objects of different classes to be treated as instances of a common_________
superclass
What is polymorphism in object-oriented programming, and how does it benefit software design?
Answer
Polymorphism allows us to use methods in a child class that have the same named method in the parent class, it benefits software design by allowing methods to be reusable by being used interchangeably in different classes, it allows the code to be used more flexibly, which lets it be maintained easily.
Encapsulation helps protect the internal state of an object from unauthorized access.
T
True
F
False
T
Classes can be instantiated multiple times to create multiple objects.
T
True
F
False
T
A constructor in Python is defined using the ___________ method.
__init__
Inheritance allows a derived class to have access to private members of the base class by default.
T
True
F
False
F
Field
Variable that stores the state of an object.
Variable that stores the state of an object.
Object
Instance of a class.
Interface
A common method signature for implementation.
Decorator
Decorator
1
Single Inheritance
A subclass inherits from one superclass.
2
Multilevel Inheritance
A subclass inherits from another subclass.
Abstract Method
Declared but not implemented in the abstract class.
Concrete Method
Fully implemented method that can be used.
In Python, all methods must have a return type specified.
T
True
F
False
F
Which of the following is NOT a principle of object-oriented programming?
Abstraction.
Multiple Inheritance.
Encapsulation.
Polymorphism.
Multiple Inheritance.
Encapsulation
Using private variables with getters and setters.
2
Inheritance
A Dog class inherits from an Animal class.
3
Polymorphism
Different shapes implementing a draw() method.
4
Constructor Overloading
Using default parameters in constructors.
The ______ parameter in Python methods refers to the instance of the class.
self
Encapsulation involves bundling data and methods within a single unit called a _______
class.
Explain the concept of encapsulation in object-oriented programming. How does it contribute to the security and maintainability of code?
Answer
Encapsulation is the bundling of data and methods that operate in a single unit. Encapsulation contributes to security and maintainability of code by restricting access of the data, making it harder for outsiders to get access to the data.
Abstraction
Simplifying complex reality.
2
Method Overriding
Subclass provides specific implementation for an inherited method.
3
Data Hiding
Restricting access to certain attributes.
4
Dynamic Typing
Variables can change data type.
Inheritance allows a class to inherit properties and behaviors from another class, referred to as the
base class
class
2
def
3
super()
4
@property
Defines a new class.
Defines a new method.
Calls a method from the superclass.
Creates a getter method.
Classes
2
Encapsulation
3
Inheritance
4
Methods
Blueprints for creating objects.
Hiding the internal state of an object.
Acquiring properties from another class.
Functions that define the behavior of objects.
An abstract class cannot be instantiated directly and serves as a ____________ for other classes.
blueprint
What is the Syntax to create a Frame ?
win.frame(options)
Frame(window,options)
Both of the above
None of the above
Frame(window,options)
Correct way to draw a line in canvas tkinter ?
create_line(canvas)
line()
canvas.create_line()
None of the above
canvas.create_line()
Which of the following is correct ?
GUI is the part of the canvas
Both of the above
None of the above
canvas is the part of the GUI
canvas is the part of the GUI
It is a container used for grouping and organizing the widgets.
All of the above
canvas
window
frame
frame
How we import a tkinter in python program ?
from tkinter import *
import tkinter
import tkinter as t
All of the above
All of the above
Creating line are come in which type of thing ?
GUI
Canvas
None of the above
Both of the above
Canvas
A window is an instance of tkinter’s Tk class.
T
True
F
False
T
True
How pack() function works on tkinter widget ?
None of the above
According to left,right,up,down
According to row and column vise
According to x,y coordinate
According to left,right,up,down
Which of the following geometry managers are available in tkinter? choose 3 answers
.grid()
.flex()
.table()
.place()
.pack()
.grid()
.place()
.pack()
What we use to change the back ground color any widget ?
background
bground
bg
fg
bg
Tkinter tool in python provide the
GUI
Database
OS commands
All of the above
GUI
How the grid() function put the widget on the screen ?
None of the above
According to x,y coordinate
According to row and column vise
According to left,right,up,down
According to row and column vise
It is a method we use to hold the screen waiting for events to happen ?
mainloop() function
Stop() function
None of the above
pause() function
Q
mainloop() function
How the place() function put the widget on the screen ?
According to x,y coordinate
According to row and column vise
None of the above
According to left,right,up,down
According to x,y coordinate
What is the use of the pack() function for the tkinter widget ?
To define a size of the widget
To perform a task by a widget
To organize the widgets in blacks before placing them on the screen
To destroy the widget
To organize the widgets in blacks before placing them on the screen
To change the color of the text in the Label widget, what we use ?
bg
cchng
color
fg
fg
fg in tkinter widget is stands for ?
foreground
forgap
forgrid
background
a
Essential thing to create a window screen using tkinter python?
To define a geometry
All of the above
call tk() function
create a button
c
For what purpose, the bg is used in Tkinter widget ?
To change the color of widget
To change the size of widget
To change the background of widget
To change the bordergrid of widget
c
GUI stands for
Global user interaction
Graph user interaction
Graphical user interaction
Graphical user interface
D
Which of the correct way to set the geometry of the window ?
geometry(x y)
geometry(“300x400”)
geometry(300,400)
geometry(300;400)
B
To get the multiple line user data, which widget we use ?
Entry
Text
Label
Canvas
B
Which of the following function are used to get the data from the Entry field in Python Tkinter ?
get()
gettext()
getdata()
getEntry()
A
Which of the following is used to call a function by the Button widget in tkinter python ?
call
command
contact
execute
B
To delete any widget from the screen which function we use ?
stop()
delete()
destroy()
break()
C
What is the correct syntax of destroy in tkinter ?
destroy(object)
object.destroy()
object(destroy)
delete(object)
B
Which of the following we can able to delete using destroy() function ?
I. Button
II. Label
III. Frame
I only
I and II
II and III
I, II and III
D
Text widget refers to the multi-line and non-editable text.
T
True
F
False
F
Which of the following is clickable in GUI programming ?
I. Button
II. Checkbutton
III. Label
I only
I and II
II and III
I, II, and III
B