0% found this document useful (0 votes)
133 views

Python Programming KNC 302-1-2020

This document is a question paper for a Python programming exam consisting of 3 sections: Section A contains 10 short answer questions worth 2 marks each about Python concepts like the raise statement, recursive functions, range(), with construct, errors in variable assignment, operator precedence, user input, errors in list indexing, lambda functions, and the use of __lt__() in classes. Section B contains 5 long answer questions worth 10 marks each, requiring students to write Python functions to remove a character from a string, calculate the average of a list handling empty lists, explain linear vs binary search, find numbers in a list less than a given value using list comprehension, and find all positive divisors of a number. Section C contains

Uploaded by

SAKSHI CHAUHAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

Python Programming KNC 302-1-2020

This document is a question paper for a Python programming exam consisting of 3 sections: Section A contains 10 short answer questions worth 2 marks each about Python concepts like the raise statement, recursive functions, range(), with construct, errors in variable assignment, operator precedence, user input, errors in list indexing, lambda functions, and the use of __lt__() in classes. Section B contains 5 long answer questions worth 10 marks each, requiring students to write Python functions to remove a character from a string, calculate the average of a list handling empty lists, explain linear vs binary search, find numbers in a list less than a given value using list comprehension, and find all positive divisors of a number. Section C contains

Uploaded by

SAKSHI CHAUHAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Printed Page: 1 of 4 

Subject Code: KNC302


0Roll No: 0 0 0 0 0 0 0 0 0 0 0 0 0
 
B.TECH
(SEM III) THEORY EXAMINATION 2020-21
PYTHON PROGRAMMING
Time: 3 Hours Total Marks: 100
Note: 1. Attempt all Sections. If require any missing data; then choose suitably.

SECTION A
1. Attempt all questions in brief.
Q no. Question Marks CO
a. What is the use of “raise” statement? Describe with an example. 2 5

b. Write a recursive Python function “rprint” to print all elements in a 2 3


list in reverse.
rprint([]) prints nothing
rprint([1,2,3]) prints 3 2 1

c. Describe the behavior of “range (s, e)” in Python. 2 3

d. Explain the use of “with” construct in Python with an example 2 5

P
program.
0Q

1
13
e. Which of the following statements produce an error in Python? 2 1
29

x, y, z = 1,2,3 # s1

2.
0E

a, b = 4,5,6 # s2

24
P2

u = 7,8,9 # s3

5.
(List all the statements that have error.)
_Q

.5
f. Explain the role of precedence with an example. 17 2 1
TU

g. How do you read an input from a user in Python to be used as an integer 2 5


|1

in the rest of the program? Explain with an example.


AK

6
:5

h. Consider the program: 2 1


07

x = ['12', 'hello', 456]


:
14

x[0] *= 3
x[1][1] = 'bye'
1
02

Explain why this program generates an error.


-2
ar

i. What is the output of the following program? 2 5


M

(lambda x,y : y - 2*x) (1, 11)


2-
|1

j. Explain the use of lt function in a class Python? 2 5

AKTU_QP20E290QP | 12-Mar-2021
Downloaded 14:07:56 | 117.55.242.131
from : uptukhabar.net
Printed Page: 2 of 4 
Subject Code: KNC302
0Roll No: 0 0 0 0 0 0 0 0 0 0 0 0 0
 
SECTION B
2. Attempt any three of the following:
Q no. Question Marks CO
a. Write a Python function removekth(s, k) that takes as input a 10 5
string s and an integer k>=0 and removes the character at index k. If k
is beyond the length of s, the whole of s is returned. For example,

removekth(“PYTHON”, 1) returns “PTHON”


removekth(“PYTHON”, 3) returns “PYTON”
removekth(“PYTHON”, 0) returns “YTHON”
removekth(“PYTHON”, 20) returns “PYTHON”

b. Write a Python function average to compute the average of a list of 10 3


numbers. The function must use try-except to handle the case
where the input list is empty. Further, in that case the average for the
empty list should be set to 0.0 using the except block.

c. Describe the differences between a linear search and a binary search? 10 5

P
d. Write a function lessthan(lst, k) to return list of numbers less 10
0Q 4
than k from a list lst. The function must use list comprehension.

1
13
Example:
29

lessthan([1, -2, 0, 5, -3], 0) returns [-2, -3]

2.
0E

24
e. Write a program factors(N) that returns a list of all positive divisors 10 2
P2

5.
of N (N>=1). For example:
_Q

.5
factors(6) returns [1,2,3,6] 17
factors(1) returns [1]
TU

factors(13) returns [1,13]


|1
AK

6
:5

SECTION C
07

3. Attempt any one part of the following:


:

Q no. Question Marks CO


14

a. How can you create Python file that can be imported as a library as well 10 5
1

as run as a standalone script?


02
-2

b. Describe the difference between 10 5


ar

import library
and
M
2-

from library import *


when used in a python program. Here library is some python
|1

library.

Downloaded
AKTU_QP20E290QP from : uptukhabar.net
| 12-Mar-2021 14:07:56 | 117.55.242.131
Printed Page: 3 of 4 
Subject Code: KNC302
0Roll No: 0 0 0 0 0 0 0 0 0 0 0 0 0
 
4. Attempt any one part of the following:
Q no. Question Marks CO
a. Write a function makePairs that takes as input two lists of equal 10 2
length and returns a single list of same length where k-th element is the
pair of k-th elements from the input lists. For example,

makePairs([1,3,5,7],[2,4,6,8])
returns [(1,2),(3,4),(5,6),(7,8)]
makePairs([],[])
returns []

b. Show an example where both Keyword arguments and Default 10 4


arguments are used for the same function in a call. Show both the
definition of the function and its call.

5. Attempt any one part of the following:

P
Q no. Question 0Q Marks CO
a. Explain why Python is considered an interpreted language. 10 1

1
13
29

b. What is short circuit evaluation? What is printed by the following 10 1

2.
0E

Python program?

24
a = 0
P2

5.
b = 2
_Q

.5
c = 3 17
x = c or a
TU

print(x)
|1
AK

6
:5
:07
14
1
02
-2
ar
M
2-
|1

AKTU_QP20E290QP | 12-Mar-2021 14:07:56 | 117.55.242.131


Downloaded from : uptukhabar.net
Printed Page: 4 of 4 
Subject Code: KNC302
0Roll No: 0 0 0 0 0 0 0 0 0 0 0 0 0
 
6. Attempt any one part of the following:
Q no. Question Marks CO
a. Write a Python program, triangle(N), that prints a right triangle 10 3
having base and height consisting of N * symbols as shown in these
examples:

triangle(3) prints:
*
**
***

triangle(5) prints:
*
**
***
****
*****

b. Write a Python program, countSquares(N), that returns the count 10 4


of perfect squares less than or equal to N (N>1). For example:

P
countSquares(1) returns 1
0Q

1
# Only 1 is a perfect square <= 1

13
29

countSquares(5) returns 2

2.
# 1, 4 are perfect squares <= 5
0E

24
countSquares(55) returns 7
P2

# 1, 4, 9, 16, 25, 36, 49 <= 55

5.
_Q

.5
17
7. Attempt any one part of the following:
TU

|1

Q no. Question Marks CO


AK

a. Write a Python function, alternating(lst), that takes as 10 4


6
:5

argument a sequence lst. The function returns True if the elements


07

in lst are alternately odd and even, starting with an even number.
Otherwise it returns False. For example:
:
14

alternating([10, 9, 9, 6]) returns False


alternating([10, 15, 8]) returns True
1
02

alternating([10]) returns True


alternating([]) returns True
-2

alternating([15, 10, 9]) returns False


ar
M
2-

b. Write a Python function, searchMany(s, x, k), that takes as 10 3


argument a sequence s and integers x, k (k>0). The function returns
|1

True if there are at most k occurrences of x in s. Otherwise it returns


False. For example:
searchMany([10, 17, 15, 12], 15, 1) returns True
searchMany([10, 12, 12, 12], 12, 2) returns False
searchMany([10, 12, 15, 11], 17, 18) returns True

 
 

AKTU_QP20E290QP | 12-Mar-2021
Downloaded 14:07:56 | 117.55.242.131
from : uptukhabar.net

You might also like