PlusTwoCompSci MLM EM
PlusTwoCompSci MLM EM
School, Theni
Plus Two Computer Science
Chapter 1-Function
1. The small sections of code that are used to perform a particular task is called
2. Which of the following is a unit of code that is often defined within a greater code structure?
6. Which of the following are mandatory to write the type annotations in the function definition?
(A) Curly braces (B) Parentheses (C) Square brackets (D) indentations
8. Which of the following carries out the instructions defined in the interface?
9. The functions which will give exact result when same arguments are passed are called
10. The functions which cause side effects to the arguments passed are called
1. Which of the following functions that build the abstract data type ?
2. Which of the following functions that retrieve information from the data type?
9. Which of the following allow to name the various parts of a multi-item object?
10. Which of the following is constructed by placing expressions within square brackets?
Chapter 3- Scoping
Choose the best answer (1 Mark)
1. Which of the following refers to the visibility of variables in one part of a program to another
part of the same program.
(A) Scope (B) Mapping (C) late binding (D) early binding
3. Which of the following is used in programming languages to map the variable and object?
(A) Local Scope (B) Global scope (C) Module scope (D) Function Scope
7. Which of the following security technique that regulates who cause resources in a computing
environment?
8. Which of the following members of a class can be handled only from within the class?
(A) Public members (B)Protected members (C) Secured members (D) Private members
(A) Public members (B)Protected members (C) Secured members (D) Private members
10. The members that are accessible from within the class and are also available to its sub-
classes is called
(A) Public members (B)Protected members (C) Secured members (D) Private members
1 .The word comes from the name of a Persian mathematician Abu Ja’far Mohammed ibn-i Musa al
Khowarizmi is called?
2. From the following sorting algorithms which algorithm needs the minimum number of swaps?
(A) Bubble sort (B) Quick sort (C) Merge sort (D) Selection sort
5. From the following sorting algorithms which has the lowest worst case complexity?
(A) Bubble sort (B) Quick sort (C) Merge sort (D) Selection sort
9. If a problem can be broken into sub problems which are reused several times, the problem
possesses which property?
(A) Overlapping sub problems (B) Optimal substructure (C) Memoization (D) Greedy
10. In dynamic programming, the technique of storing the previously calculated values is called ?
5. This symbol is used to print more than one item on a single line.
A) Semicolon(;) B) Dollar($) C) comma(,) D) Colon(:)
A) 3 B) 4 C) 5 D) 6
1.A named blocks of code that are designed to do one specific job is called as
9.Pick the correct one to execute the given statement successfully. if ____ : print(x, " is a leap year")
10. Which of the following keyword is used to define the function testpython(): ?
2.What will be the output of the following code? str1 = "Chennai Schools" str1[7] = "-"
(a) Chennai-Schools (b) Chenna-School (c) Type error (D) Chennai
5.Strings in python:
7.What is stride?
(a) index value of slide operation (b) first argument of slice operation
(c) second argument of slice operation (d) third argument of slice operation
8.Which of the following formatting character is used to print exponential notation in upper case?
9.Which of the following is used as placeholders or replacement fields which get replaced along with
format( ) function?
(a) Positive (b) Negative (c) Both (a) and (b) (d) Either (a) or (b)
3.Which of the following function is used to count the number of elements in a list?
6.Which of the following Python function can be used to add more than one element within an
existing list?
(a) append() (b) append_more() (c)extend() (d) more()
7. What will be the result of the following Python code? S=[x**2 for x in range(5)] print(S)
11.Which of the following set operation includes all the elements that are in two sets but not the one
that are common to two sets?
1.Which of the following are the key features of an Object Oriented Programming language?
(a) Constructor and Classes (b) Constructor and Object
(c) Classes and Objects (d) Constructor and Destructor
8.Which of the following is the output of the following program? class Student:
def __init__(self, name):
self.name=name
S=Student(“Tamil”)
2 A table is known as
a) tuple b) attribute c) relation d)entity
N.S.Hr.Sec.School, Theni Page 9
3 Which database model represents parent-child relationship?
a) Relational b) Network c) Hierarchical d) Object
1.Which commands provide definitions for creating table structure, deleting relations, and modifying
relation schemas.
a. DDL b. DML c. DCL d. DQL
(A) Flat File (B) 3D File (C) String File (D) Random File
(A) Control Return and Line Feed (B) Carriage Return and Form Feed
(C) Control Router and Line Feed (D) Carriage Return and Line Feed
3.Which of the following module is provided by Python to do several operations on the CSV files?
4.Which of the following mode is used when dealing with non-text files like image or exe files?
(A) Text mode (B) Binary mode (C) xls mode (D) csv mode
6.Which of the following is a string used to terminate lines produced by writer()method of csv
module?
(A) Line Terminator (B) Enter key (C) Form feed (D) Data Terminator
9.Making some changes in the data of the existing file or adding more data is called
10.What will be written inside the file test.csv using the following program import csv
D = [['Exam'],['Quarterly'],['Halfyearly']]
csv.register_dialect('M',lineterminator = '\n')
with open('c:\pyprg\ch13\line2.csv', 'w') as f:
wr = csv.writer(f,dialect='M')
wr.writerows(D)
f.close() Prepared By
S.Ganesh Kumar, B.Sc., B.Ed., M.S.I.T.,
5.Which of the following is a software design technique to split your code into separate parts?
(A) Object oriented Programming (B) Modular programming
(C) Low Level Programming (D) Procedure oriented Programming
6.The module which allows you to interface with the Windows operating system is
(A) OS module (B) sys module (c) csv module (d) getopt module
(A) argv variable (B) opt variable (c)args variable (d) ifile variable
3. Which of the following is a control structure used to traverse and fetch the records of the
database?
4. Any changes made in the values of the record should be saved by the command
5.Which of the following executes the SQL command to perform some action?
6. Which of the following function retrieves the average of a selected column of rows in a table?
7.The function that returns the largest value of the selected column is
C:\Users\YourName\AppData\Local\Programs\Python\Python36-32\Scripts>pip – version
a. Check if PIP is Installed b. Install PIP c. Download a Package d. Check PIP version
4.Read the following code: Identify the purpose of this code and choose the right option from the
following.
5.To install matplotlib, the following function will be typed in your command prompt. What does “-
U”represents?
Python –m pip install –U pip
a. downloading pip to the latest version b. upgrading pip to the latest version
c. removing pip d. upgrading matplotlib to the latest version
6. Observe the output figure. Identify the coding for obtaining this output.
Answer: (C)
Hint 1: This chart is often used to visualize a trend in data over intervals of time.
Hint 2: The line in this type of chart is often drawn chronologically.
10. Read the statements given below. Identify the right option from the following for pie chart.
Statement A: To make a pie chart with Matplotlib, we can use the plt.pie() function.
Statement B: The autopct parameter allows us to display the percentage value using the Python string
formatting.
ffect
ff
2) What is the side eff of impure function. Give example. (L1)
If a function modifies the variable outside the function is called as side effect and such
functions are called as impure functions. For example the mathematical function random() will give
different outputs for the same function call.
fferentiate pure and impure functions. (L1)
ff
3) Diff
Pure Function Impure Function
The return value of the pure functions solely The return value of the impure functions does
depends on its arguments passed. not solely depend on its arguments passed.
Hence, if you call the pure functions with the Hence, if you call the impure functions with the
same set of arguments, you will always get same set of arguments, you might get the different
the same return values. return values For example, random().
They do not modify the arguments which They may modify the arguments which are passed
are passed to them to them
fferentiate
ff
5) Diff constructors and selectors. (L2)
Constructors are functions that build the abstract data type. Selectors are functions that
retrieve information from the data type.
6) What is a Pair? Give an example. (L2)
Any way of bundling two values together into one can be considered as a pair. Lists are a
common method to do so. Therefore List can be called as Pairs.
Ex: lst[(0, 10), (1, 20)]
N.S.Hr.Sec.School, Theni Page 15
7) What is a scope? (L3)
Scope refers to the visibility of variables, parameters and functions in one part of a program to
another part of the same program. In other words, which parts of your program can see or use it.
9) How Python represents the private and protected Access specifies? (L3)
Python prescribes a convention of prefixing the name of the variable/method with single or
double underscore to emulate the behaviour of protected and private access specifies.
fferent
ff
14) What are the diff modes that can be used to test Python Program ? (L5)
In Python, programs can be written in two ways namely Interactive mode and Script mode.
The Interactive mode allows us to write codes in Python command prompt (>>>) whereas in script
mode programs can be written and stored as separate file with the
extension .py and executed.
fferentiate
ff
4) Diff Concrete data type and abstract datatype. (L2)
Concrete data type Abstract data type
A concrete data type is s data type whose An abstract data type is s data type whose
representation is known representation is unknown
Concrete data types or structures are direct Abstract data type offer a high level view
implementation of relatively simple concept. (use) of a concept independent of its
implementation.
fferent
ff
5) What are the diff ways to access the elements of a list. Give example. (L2)
The elements of a list can be accessed in two ways.
The first way is via our familiar method of multiple assignment. For example,
lst := [10, 20]
x, y := lst
In the above example x will become10 and y will become 20
A second method for accessing the elements in a list is by the element selection operator. For
example, lst[0] returns 10 and lst[1] returns 20
12) What are the assignment operators that can be used in Python? (L5)
In Python, = is a simple assignment operator to assign values to variable. Let a = 5 and b = 10
assigns the value 5 to a and 10 to b these two assignment statement can also be given as a,b=5,10
that assigns the value 5 and 10 on the right to the variables a and b respectively. There are various
compound operators in Python like +=, -=, *=, /=, %=, **= and //= are also available.
fferences
ff
17) List the diff between break and continue statements. (L6)
Python break statement
The break statement terminates the loop containing it. Control of the program flows to the
statement immediately after the body of the loop.
Python continue statement
The continue statement is used to skip the rest of the code inside a loop for the current iteration
only. Loop does not terminate but continues on with the next iteration.
18) Write the rules of local variable. (L7)
1. A variable with local scope can be accessed only within the function/block that it is created in.
2. When a variable is created inside the function/block, the variable becomes local to it.
3. A local variable only exists while the function is executing.
4. The formate arguments are also local to function.
22) Explain the following built-in functions. (a) id( ) (b) chr( ) (c) round( ) (d) type( ) (e) pow() (L7)
id (object) Returns the address of the object x=15 address of x is :
in memory print ('address of x :',id (x)) 1357486752
type (object) Returns the type of object for the x= 15.2 <class 'float'>
given single object. print (type (x))
round(number Returns the nearest integer to its x = round(5.76543, 2) 5.77
[,ndigits]) input. print(x)
pow (x,y) Returns the value of x to the power x = pow(4, 3) 64
of y (xy) print(x)
chr() Returns the Unicode character for x = chr(97) a
the given ASCII value. print(x)
23) Write a short about the followings with suitable example: (a) capitalize( ) (b) swapcase( ) (L8)
Prepared By
S.Ganesh Kumar, B.Sc., B.Ed., M.S.I.T.,
Parameters are the variables in a function definition and arguments are the values which are passed
to a function definition.
(requires: b>=0 )
(returns: a to the power of b)
let rec pow a b:=
if b=0 then 1
else a * pow a (b-1)
In the above function definition variable ‘b’ is the parameter and the value which is passed to the
variable ‘b’ is the argument. Note we have not mentioned any types: (data types). Some language
compiler solves this type (data type) inference problem algorithmically, but some require the type to
be mentioned.
(requires: b> 0 )
(returns: a to the power of b )
let rec pow (a: int) (b: int) : int :=
if b=0 then 1
else a * pow b (a-1)
2) What is a List? Why List can be called as Pairs. Explain with suitable example (L2)
List is constructed by placing expressions within square brackets separated by commas. Such
an expression is called a list literal. List can store multiple values. Each value can be of any type and
can even be another list.
The elements of a list can be accessed in two ways. Te first way is via our familiar method of
multiple assignment.
A second method for accessing the elements in a list is by the element selection operator, also
expressed using square brackets.
Any way of bundling two values together into one can be considered as a pair. Lists are a common
method to do so. Therefore List can be called as Pairs.
3) How will you facilitate data abstraction. Explain it with suitable example (L2)
Data abstraction replicate how we think about the world. For example, when you want to drive a
car, you don’t need to know how the engine was built or what kind of material the tires are made of.
You just have to know how to turn the wheel and press the gas pedal. To facilitate data abstraction,
you will need to create two types of functions: constructors and selectors.
For example, say you have an abstract data type called city. This city object will hold the city’s
name, and its latitude and longitude. To create a city object, you’d use a function like
city = makecity (name, lat, lon)
The following pseudo code will compute the distance between two city objects:
As you already know that Constructors are functions that build the abstract data type. In the
above pseudo code the function which creates the object of the city is the constructor.
Here makecity (name, lat, lon) is the constructor which creates the object city.
Selectors are nothing but the functions that retrieve information from the data type. Therefore in the
above code
• getname(city)
• getlat(city)
• getlon(city)
are the selectors because these functions extract the information of the city object.
N.S.Hr.Sec.School, Theni Page 23
4) Explain the types of scopes for variable or LEGB rule with example. (L3)
Scope refers to the visibility of variables, parameters and functions in one part of a program to
another part of the same program. The LEGB rule is used to decide the order in which the scopes are
to be searched for scope resolution.
Local Scope
Local scope refers to variables defend in current function. Always, a function will first look
up for a variable name in its local scope. Only if it does not find it there, the outer scopes are
checked.
Enclosed Scope
A variable which is declared inside a function which contains another function definition with
in it, the inner function can also access the variable of the outer function. This scope is called
enclosed scope.
Global Scope
A variable which is declared outside of all the functions in a program is known as global
variable. This means, global variable can be accessed inside or outside of all the functions in a
program.
Built-in Scope
Any variable or module which is defend in the library functions of a programming language has
Built-in or module scope. They are loaded as soon as the library files are imported to the program.
o If the target value is equal to the middle element of the array, then return the index of
the middle element.
o If not, then compare the middle element with the target value,
For a binary search to work, it is mandatory for the target array to be sorted. The following is our
sorted array and let us assume that we need to search the location of value 31 using binary
search.
Now we compare the value stored at location 4. We find that the value at location 4 is 27, which
is not a match. As the value is greater than 27, we know that the target value must be in the
upper portion of the array.
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.
The value stored at location 7 is not a match, rather it is more than what we are looking for. So,
the value must be in the lower part from this location.
Bubble sort is based on the idea of repeatedly comparing pairs of adjacent elements and then
swapping their positions if they exist in the wrong order.
Following are the steps involved in bubble sort(for sorting a given array in ascending order):
1) Starting with the first element(index = 0), compare the current element with the next element of
the array.
2) If the current element is greater than the next element of the array, swap them.
3) If the current element is less than the next element, move to the next element. Repeat Step 1.
10) Explain the concept of Dynamic programming with suitable example. (L4)
Dynamic programming is an algorithmic design method that can be used when the solution to a
problem can be viewed as the result of a sequence of decisions. Dynamic programming approach is
N.S.Hr.Sec.School, Theni Page 27
similar to divide and conquer. The given problem is divided into smaller and yet smaller possible
sub-problem.
Steps to do Dynamic programming
• The given problem will be divided into smaller overlapping sub-problems.
• An optimum solution for the given problem can be achieved by using result of smaller sub-
problem.
Dynamic programming approach : Fibonacci Series
The following example shows a simple Dynamic programming approach for the generation of
Fibonacci series.
Initialize f0=0, f1 =1
step-1: Print the initial values of Fibonacci f0 and f1
step-2: Calculate fbanocci fb ← f0 + f1
step-3: Assign f0← f1, f1← fb
step-4: Print the next consecutive value of fbanocci fb
step-5: Goto step-2 and repeat until the specified number of terms generated.
For example if we generate fibobnacci series upto 10 digits, the algorithm will generate
the series as shown below: 0 1 1 2 3 5 8 13 21 34 55
12) Write a detailed note on if..else..elif statement with suitable example. (L6)
When we need to construct a chain of if statement(s) then ‘elif ’ clause can be used
instead of ‘else’.
if <condition-1>:
statements-block 1
elif <condition-2>:
statements-block 2
else:
statements-block n
In the syntax of if..elif..else mentioned above, condition-1 is tested if it is true then
statements-block1 is executed, otherwise the control checks condition-2, if it is true statements
block2 is executed and even if it fails statements-block n mentioned in else part is executed.
Example:
num=int(input(“Enter a number “))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
15) Explain about string operators in python with suitable example. (L8)
Python provides the following operators for string operations. These operators are useful to
manipulate string.
Example:
for i in range (2,10,2):
print (i, end=' ')
Output: 2 4 6 8