• Tidak ada hasil yang ditemukan

Chapter 6 Functions

N/A
N/A
Protected

Academic year: 2025

Membagikan "Chapter 6 Functions"

Copied!
37
0
0

Teks penuh

(1)

Sample Exam Questions

Final Exam – Part 1 (MCQ)

CPIT 110 (Problem-Solving and Programming)

Version 1.2

(2)

هيبنت

!

2

سلأع ذذب سلأابعذذل لأاذذيبتخلأاذذلئتلأ ذذيرلأاذذنيةلأقذذةلأيع ذذبةلأابعذذلئسلأا ذذه • لأ ذذهن

- .) CPIT-110 ( لأتلاكشم سلألحولأاجمرب سلأعريم لأ) تسع ي س ( لأ ن ث سلأءزج س يركس مب لأ هيبةلأدم عُبلأ لأابعلئسلأا ه • .

ع ب لا لأيعريم سلأعيلسئم سلأعيمجلأابعلئسلأا هلألمشتلأ لأدق • .

سلأ ذيبقتولأيركس ذملأقذملأء ذه ن سلأدعبلأاعجسرمب لأابل نملأابعلئسلأا ه • لأعيذلسئم

ع ب لا لأيعريم س .

فبم سلأس هلأت حفصلأاب هنلأايررملأابعلئسلألئبح •

.

(3)

هيبنت

!

3

لأقملأً يب للأه لسعدلأمتلأ ملأعيمجلألم شلأع ب س • Chapter 1

لأىلإ Chapter 6 .

ه ذذمنلأعذذيمجلأاذذعجسرملأئذذجرنولأ ذذيرلأ د ذذو سلأق ذذب سلأابعذذل لأهذذبلأ هئذذمن سلأس ذذه •

لأ لأ. ايب و سلأقسئبلأ لأايب و سلأتسع ب س

(4)

Chapter 1, 2, 3:

Sample Exam Questions

Mid-Term Exam 1 – Part 1 (MCQ) Chapter 4, 5:

Sample Exam Questions

Mid-Term Exam 2 – Part 1 (MCQ)

https://csu.kau.edu.sa/Pages-cpit110-test-questions.aspx

4

(5)

Chapter 6: Functions Questions

5

(6)

Question

#1

6

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

a) None

b) Integer c) Float d) String

Section 6.2 Defining a Function

(7)

Question

#2

7

The header of a function consists of ____________.

a) function name

b) function name and parameter list c) parameter list

d) return statement

Section 6.2 Defining a Function

(8)

Question

#3

8

A function _________.

a) must have at least one parameter b) may have no parameters

c) must always have a return statement to return a value

d) must always have a return statement to return multiple values

Section 6.2 Defining a Function

(9)

Question

#4

9

Arguments to functions always appear within __________.

a) brackets

b) parentheses c) curly braces

d) quotation marks

Section 6.3 Calling a Function

(10)

Question

#5

10

Does the function call in the following function cause syntax errors?

import math def main():

math.sin(math.pi) main()

a) Yes b) No

Section 6.3 Calling a Function

(11)

Question

#6

11

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) a heap

b) storage area c) a stack

d) an array

Section 6.3 Calling a Function

(12)

Question

#7

12

Which of the following should be defined as a None function?

a) Write a function that prints integers from 1 to 100.

b) Write a function that returns a random integer from 1 to 100.

c) Write a function that checks whether a number is from 1 to 100.

d) Write a function that converts an uppercase letter to lowercase.

Section 6.4 Functions With/Without Return Values

(13)

Question

#8

13

A function with no return statement returns ______.

a) void b) 1

c) 0

d) None

Section 6.4 Functions With/Without Return Values

(14)

Question

#9

14

Consider the following incomplete code:

def f(number):

# Missing function body print(f(5))

The missing function body should be ________.

a) return "number"

b) print(number) c) print("number") d) return number

Section 6.4 Functions With/Without Return Values

(15)

Question

#10

15

Given the following function header:

def f(p1, p2, p3, p4)

Which of the following is correct to invoke it?

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, p2 = 4)

Section 6.5 Positional and Keyword Arguments

(16)

Question

#11

16

Given the following function header:

def f(p1, p2, p3, p4)

Which of the following is correct to invoke it?

a) f(1, 2, 3)

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)

Section 6.5 Positional and Keyword Arguments

(17)

Question

#12

17

Given the following function header:

def f(p1, p2, p3, p4)

Which of the following is correct to invoke it?

a) f()

b) f(p1 = 1, 2, 3, 4)

c) f(p1 = 1, p2 = 2, p3 = 3, 4) d) f(1, 2, 3, p4 = 4)

Section 6.5 Positional and Keyword Arguments

(18)

Question

#13

18

Given the following function:

def nPrint(message, n):

while n > 0:

print(message, end="") n -= 1

What will be displayed by the call nPrint('a', 4)?

a) aaaaa b) aaaa c) aaa

d) invalid call e) infinite loop

Section 6.5 Positional and Keyword Arguments

(19)

Question

#14

19

Given the following function:

def nPrint(message, n):

while n > 0:

print(message) n -= 1

What will be displayed by the call nPrint('a', 4)?

a) aaaaa b) aaaa c) aaa

d) invalid call e) infinite loop

Section 6.5 Positional and Keyword Arguments

(20)

Question

#15

20

Given the following function:

def nPrint(message, n):

while n > 0:

print(message) n -= 1

What is k after invoking nPrint("A message", k)?

k = 2

nPrint("A message", k) a) 0

b) 1 c) 2 d) 3

Section 6.5 Positional and Keyword Arguments

(21)

Question

#16

21

Given the following function:

def nPrint(message, n):

while n > 0:

print(message) n -= 1

What is k after invoking nPrint("A message", k)?

k = 2

nPrint(n = k, message = "A message") a) 0

b) 1 c) 2 d) 3

Section 6.5 Positional and Keyword Arguments

(22)

Question

#17

22

A variable defined inside a function is referred to as __________.

a) a global variable b) a function variable c) a block variable

d) a local variable

Section 6.9 The Scope of Variables

(23)

Question

#18

23

A variable defined outside a function is referred to as __________.

a) a global variable b) a function variable c) a block variable

d) a local variable

Section 6.9 The Scope of Variables

(24)

Question

#19

24

Whenever possible, you should avoid using __________.

a) global variables

b) function parameters c) global constants

d) local variables

Section 6.9 The Scope of Variables

(25)

Question

#20

25

What will be displayed by the following code?

x = 1

def f1():

y = x + 2

print(y, end=" ") f1()

print(x)

a) 1 3 b) 3 1

c) The program has a runtime error because x is not defined.

d) 1 1 e) 3 3

Section 6.9 The Scope of Variables

(26)

Question

#21

26

What will be displayed by the following code?

x = 1

def f1():

x = 3

print(x, end=" ") f1()

print(x)

a) 1 3 b) 3 1

c) The program has a runtime error because x is not defined.

d) 1 1 e) 3 3

Section 6.9 The Scope of Variables

(27)

Question

#22

27

What will be displayed by the following code?

x = 1

def f1():

x = x + 2

print(x, end=" ") f1()

print(x)

a) 1 3 b) 3 1

c) The program has a runtime error because x is not defined.

d) 1 1 e) 3 3

Section 6.9 The Scope of Variables

(28)

Question

#23

28

What will be displayed by the following code?

x = 1

def f1():

global x x = x + 2

print(x, end=" ") f1()

print(x)

a) 1 3 b) 3 1

c) The program has a runtime error because x is not defined.

d) 1 1 e) 3 3

Section 6.9 The Scope of Variables

(29)

Question

#24

29

What will be displayed by the following code?

def f1(x = 1, y = 2):

x = x + y y += 1

print(x, y) f1()

a) 1 3 b) 3 1

c) The program has a runtime error because x and y are not defined.

d) 1 1 e) 3 3

Section 6.10 Default Arguments

(30)

Question

#25

30

What will be displayed by the following code?

def f1(x = 1, y = 2):

x = x + y y += 1

print(x, y) f1(2, 1)

a) 1 3 b) 2 3

c) The program has a runtime error because x and y are not defined.

d) 3 2 e) 3 3

Section 6.10 Default Arguments

(31)

Question

#26

31

What will be displayed by the following code?

def f1(x = 1, y = 2):

x = x + y y += 1

print(x, y) f1(y = 2, x = 1)

a) 1 3 b) 2 3

c) The program has a runtime error because x and y are not defined.

d) 3 2 e) 3 3

Section 6.10 Default Arguments

(32)

Question

#27

32

Which of the following function headers is correct?

a) def f(a = 1, b):

b) def f(a = 1, b, c = 2):

c) def f(a = 1, b = 1, c = 2):

d) def f(a = 1, b = 1, c = 2, d):

Section 6.10 Default Arguments

(33)

Question

#28

33

What will be displayed by the following code?

def hello():

print("Hello")

def hello(name = "Ahmad"):

print("Hi", name) hello()

a) Hello b) Hi

c) Hi Ahmad

d) Hello Ahmad

Section 6.10 Default Arguments

(34)

Question

#29

34

What will be displayed by the following code?

def hello():

print("Hello")

def hello(name = "Ahmad", age):

print("Name:", name, ", Age:", age) hello(24)

a) Name: Ahmad, Age: 24 b) Name: None, Age: 24 c) Name: , Age: 24

d) Syntax Error

Section 6.10 Default Arguments

(35)

Question

#30

35

What will be displayed by the following code?

def f1(x = 1, y = 2):

return x + y, x - y x, y = f1(y = 2, x = 1) print(x, y)

a) 1 3 b) 3 1

c) The program has a runtime error because the function returns the multiple values

d) 3 -1 e) -1 3

Section 6.11 Returning Multiple Values

(36)

Solutions

36

Question # Correct Answer

1 A

2 B

3 B

4 B

5 B

6 C

7 A

8 D

9 D

10 A

Question # Correct Answer

11 D

12 D

13 B

14 E

15 C

16 C

17 D

18 A

19 A

20 B

(37)

Solutions

37

Question # Correct Answer

21 B

22 C

23 E

24 E

25 D

26 E

27 C

28 C

29 D

30 D

Referensi

Dokumen terkait

In the thesis [Tanaka, 2005] it is suggested that the nine coherence axioms given in that paper are incomplete, in the sense that one of the coherence axioms is missing , a

Given previous controversial findings con- cerning possible gender bias of the GMAT, we sought to investigate the fol- lowing question: Is there bias, and, if so, does

Also, since a JIT compiler knows the exact current state of executable code, they can also optimize the code by in-lining small function calls (like replacing body of small

1. derive formulas for approximating the first derivative of a function, 2. derive formulas for approximating derivatives from Taylor series, 3. derive finite difference

Question 1: Cobb-Douglas Production Function From Gujarati 2003 Consider the following Cobb-Douglas production function Yi = 0Li1Ki2e"i where Y = output L= labour input K =capital

Graphical summary Python code Library Circular dot plot import numpy as np import matplotlib.pyplot as plot # Define the circular dot plot function def circplotdata:

For experimental group, studets are given KWL Know – Want – Learn strategy, whereas for control group is given QRST Question – Read – State - Test strategy as the treatment in teaching

The conservation of angular momentum Principle: The total angular momentum of a rotating body remains constant when the net torque acting on it is zero, and thus the angular