CS1400 exam two

¡Supera tus tareas y exámenes ahora con Quizwiz!

Given the string s = "Welcome", what is s.count ('e')??

2

Given a string s = "Programming is fun", what is s.find('ram')??

4

What is len("Good")??

4

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

Conclusion: The program runs fine and prints 1.

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

Conclusion: The program runs fine and prints 45.

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

Conclusion: The program would run if you change print(s) to print(self.s).

Given a string s = "Programming is fun", what is s.startswith( 'm' )??

False

Given a string s = "Programming is fun", what is s.endswith( 'm' )??

False

What is "Good". replace ("o", "e")??

Geed

Syntax errors or nah?? import math def main( ): math.sin(math.pi) main( )

No, this statement just does not have a return value

A function with no return statement returns _______.

None

If a function does not return a value, by default, it returns _______.

None

What is "Programming is fun" [:2]??

Pr

What is "Programming is fun" [: -1]??

Programming is fu

class Name: def __init__(self, firstName, mi, lastName): self.firstName = firstName self.mi = mi self.lastName = lastName firstName = "John" name = Name(firstName, 'F', "Smith") firstName = "Peter" name.lastName = "Pan" print(name.firstName, name.lastName)

The program displays John Pan.

T or F: "Write a function that prints integers from 1 to 100" should be defined as a None function.

True

T or F: A reference variable refers to an object.

True

T or F: A string is an immutable type.

True

T or F: All data in Python are objects.

True

T or F: An object may contain the reference of other objects.

True

T or F: Whenever possible, you should avoid using global variables.

True

Examples of implementing the string method: a. obj.__str__( ) b. str(obj) c. obj.str( ) d. __str__(obj)

a & b

To check whether string s1 contains s2, use _________. a. s1.__contains__(s2) b. s1 in s2 c. s1.contains(s2) d. s1.in(s2)

a & b

To return the length of string s, use ______________. a. s.__len__( ) b. len(s) c. size(s) d. s.size( )

a & b

Two ways to add s1 and s2 with a result of s3: a. s3 = s1 + s2 b. s3 = s1.add(s2) c. s3 = s1.__add(s2) d. s3 = s1.__add__(s2)

a & d

Which is appropriate to invoke in the header: def f(p1, p2, p3, p4)?? a. f(1, 2, 3, 4) b. f(p1 = 1, 2, 3, 4) c. f(p1=1, p2=2, p3=3, 4) d. f(p1=1,p2=2,p3=3,p4=4) e. f(1, 2, 3, p4 = 4)

a & d & e

What is min("Programming is fun")??

a blank space character

What is used to create an object??

a constructor

What is a variable defined outside a function referred to as??

a global variable

What is a variable defined inside a function referred to as??

a local variable

What is an object??

a representation of something in the real world ex:

Each time a function is invoked, the system stores parameters and local variables in an area of memory, known as ________, which stores elements in last- in first-out fashion.

a stack

What is a simple but incomplete version of a function called??

a stub

What is "Programming is fun"[-1]?

n

A function (may / may not) have no parameters.

may

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

object

blueprint : house class : ____________

object

What symbol is used to depict arguments of functions??

parentheses

What is "Programming is fun"[4:6]??

ra

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

s[ 3 ] s.__getitem__(3)

Which one has a greater numerical value?? zz or ZZ

zz

What is "Programming is fun" [1 : 1]??

' '

Given a string s = "Programming is fun", what is s.find( 'm' )??

6

Given a sting s = "Programming is fun", what is s.rfind( 'm' )??

7

What does ADT stand for??

Abstract Data Types

class Count: def __init__(self,count=0): self.__count = count c1 = Count(2) c2 = Count(2) print(id(c1) == id(c2), end= " ") s1 = "Good" s2 = "Good" print(id(s1) == id(s2))

Answer: False True Two objects with the same contents in the Python library actually share the same object. This behavior is not true for custom-defined immutable classes.

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

Conclusion: The program has a error cuz the constructor is invoked without an argument.

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

Conclusion: The program has an error cuz y is private and cannot be accessed outside of class.

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

Conclusion: The program has an error cuz y is private and cannot be accessed outside the class.

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

Conclusion: The program runs fine and prints "Welcome".

Given: s = "Welcome" Why is s[ 1 ] = ' r ' incorrect??

The string object is immutable.

Given a string s = "Programming is fun", what is s.endswith( 'fun' )??

True

Given a string s = "Programming is fun", what is s.startswith( 'Program' )??

True

**if you call a function and hard code a value into the function, where does that value go??

at the end of the line of code

Suppose i is 2 and j is 4, i + j is same as ________. a. i.__add(j) b. i.__add__(j) c. i.__Add(j) d. i.__ADD(j)

b. i.__add__(j)

define pass by reference

caller and callee have the same variable with the same values for the parameters

define pass by value

caller and callee have two independent variables have the same values for the parameters

An object is an instance of a __________.

class

What is an example of an ADT??

class

What is defined as a template or blueprint that only includes objects of the same type??

class

What keyword is required to define a class??

class

What is a class??

combination of data and functionality

How does python compare strings??

compares the numerical value of each letter, one at a time

What does the __init__ method do??

creates an object in the memory

The header of a function consists of _____.

function name and parameter list

Where does Python create and store objects in a separate memory space??

the heap

T or F: Python is an object-oriented language.

true

What is max("Programming is fun")??

u

What is "Programming is fun" [-3 : -1]??

un

define "pass by value"

when you invoke a function with a parameter, the value of the argument is passed to the parameter


Conjuntos de estudio relacionados

Chapter 7: Protecting Against Advanced Attacks

View Set

6. Avant-garde and spiritualism: Rietveld's Schröder House

View Set