Computer science python, Assignments of Computer science

These are some question for you to practice in python language.

Typology: Assignments

2023/2024

Available from 04/14/2024

money-15
money-15 🇮🇳

1 document

1 / 40

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1 | P a g e
XII CS QUESTION BANK FOR BRIGHT STUDENTS CHAPTER WISE SET-II
CLASS XII COMPUTE SCIENCE (QUESTION BANK - MLL)
CHAPTER -1 PYTHON REVSION TOUR -I
Q.1 State some features of Python.
Ans: - Python is a language which is Easy to use, it is Expressive, Cross Platform and an Open
Source Language.
Q.2 What is a python variable? Identify the variables that are invalid and state the reason
Class, do, while, 4d, a+
Ans: - A variable in python is a container to store data values.
a) do, while are invalid because they are python keyword
b) 4d is invalid because the name can’t be started with a digit.
c) a+ is also not valid as no special symbol can be used in name except underscore. ( _ )
Q.3 What is None?
Ans: - None keyword is used to define a null value or no value at all. None cannot be 0 or empty
string or False.
Eg: - var = None
print(var)
The above code will print None.
Q.4 How strings are represented in Python?
Ans: - Strings in Python are array of bytes representing Unicode characters. There are three ways
to represent a string data in python
1) Single Line Strings
Eg: - Str1= ‘I Start Learning PYTHON’
2) Multiline strings
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Computer science python and more Assignments Computer science in PDF only on Docsity!

XII CS QUESTION BANK FOR BRIGHT STUDENTS CHAPTER WISE SET-II

CLASS XII COMPUTE SCIENCE (QUESTION BANK - MLL)

CHAPTER -1 PYTHON REVSION TOUR -I

Q.1 State some features of Python. Ans: - Python is a language which is Easy to use, it is Expressive, Cross Platform and an Open Source Language. Q.2 What is a python variable? Identify the variables that are invalid and state the reason Class, do, while, 4d, a+ Ans: - A variable in python is a container to store data values. a) do, while are invalid because they are python keyword b) 4d is invalid because the name can’t be started with a digit. c) a+ is also not valid as no special symbol can be used in name except underscore. ( _ ) Q.3 What is None? Ans: - None keyword is used to define a null value or no value at all. None cannot be 0 or empty string or False. Eg: - var = None print(var) The above code will print None. Q.4 How strings are represented in Python? Ans: - Strings in Python are array of bytes representing Unicode characters. There are three ways to represent a string data in python –

  1. Single Line Strings Eg: - Str1= ‘I Start Learning PYTHON’
  2. Multiline strings

Eg: - Str2= “Initially I am scaring from Python Language But Now Working in Python become Easy for me.” Str3= ‘’’It is an easy Language’’’ Q.5 What are the various mutable and immutable data types in Python? Ans: - The data types which are changeable called Mutable Data Type, and those which are not changeable called immutable data types. Mutable Data types of Python are: - list, dictionary, set Immutable Data Types are: - int, float, complex, string, tuple Q.6 What is the use of indentation in Python. Explain. Ans: - Indentation helps to show the beginning and ending of a block. Eg: - X= Y= if (X>Y): print(‘Greater No is =’,X) else: print(‘Greater No is =’,Y)

Q.7 Predict the output for i in range( 1, 10, 3): print(i) Ans: - 1 4 7

Q.8 Rewrite the code after correcting errors: - if N=> print(odd) else Print("even")

Ans: - if N=>0: print(“odd”) else: print("even")

print(x, 'is an Odd Number') Output: -

Q.12 Write a program to print numbers from 1 to 10 and 10 to 1 Ans: - x=int(input('Enter 1 or 10')) if x==1: for x in range(1,11): print(x) else: for x in range(10,0,-1): print(x) Output: -

CHAPTER -2 PYTHON REVSION TOUR -II

  1. What is a string slice? How is it useful? Ans: A sub part or a slice of a string , say s , can be obtained using s[n:m] where n and m are integers. Python returns all the character at indices n, n+1, n+2 ... m- e.g. ‘Well done’ [1:4] will give ‘ell’ here index starts from 0.
  2. How are list different from strings when both are sequences? Ans: The list and string are different in following ways: (i) The lists are mutable sequence while string are immutable. (ii) String store single type of elements – all characters while list can store element belonging to different types. Eg: - S="PYTHON IS INTERPRETER BASED LANGUAGE" L=[83, 'Computer Science', 'PYTHON'] print(S) print(L) #S[2]='K' # This statement generate error as String is immutable. print(S) L[0]= L[1]='IP' print(L)
  3. What will be the output of the following code snippet? values =[ ] for i in range (1,4):

Chapter – 3 Working with Functions Q.1) Define a function? Ans: - It is a sub program that perform some task on the data and return a value. Q.2) Write a python function that takes two numbers and find their product. Ans: - def PRODUCT(X,Y): return (XY)* Q.3) Write a python function that takes two numbers and print the smaller number. Also write how to call this function. Ans: - def SMALER(A,B): if(A

Ans: - def SI(p, r, t=2): return(prt) /** Q.6) Write a small python function that receive two numbers and return their sum, product, difference and multiplication and modulo division. Ans: - def ARITHMATIC_OPERTAIONS(a,b): return a+b, ab, a-b, a/b, a%b calling of function: -* a,b,c,d,e=ARITHMATIC_OPERTAIONS(3,4) print('Sum=',a) print('Product=',b) print('Difference=',c) print('Division=',d) print('Modulo Division=',e)

Q.7) What is scope of a variable? Ans: - Part of program within which a name is legal and accessible is called scope of the variable. Q.8) Explain two types of variable scope with example. Ans – Global Scope – A name declared in top level segment of a program is said to have global scope and it is accessible inside whole programme. Local Scope – A name declared in a function body is said to have local scope and it can be used only within this function. A=0 # global scope

Chapter-4 Python Library Q.1) What is Libraries in Python and How many types of main library use in Python? Ans: -A Library is a collection of modules that together caters to specific types of needs or applications. Like NumPy library of Python caters to scientific computing needs. Q.2) How we can import library in Python program? Ans: - We can import a library in python program with the help of import command. Eg: - import random import pandas as pd Q.3) Give the basic structure of user defined function. Ans: - def (arguments): Statements Eg: - def MEAN(x,y): Z=(x+y)/ Print(Z) Q.4) Create a function in Python to calculate and return Area of rectangle when user enters length and bredth. Ans: - def AREA(a,b): Return(ab)* Q.5) What is module in Python? Ans: - A module is a file with .py extension that contains variables, class definitions, statements and functions related to a particular task. Q.6) Write the features of a module. Ans: - **1) Modules can be reused in other programmes

  1. It is independent group of code and data** Q.7) Create a package Arithmetic Operations(named AO) contain sum, product and difference of two numbers and use it in your main programme. Ans: - def ADD(X,Y):

return (X+Y) def PRODUCT(X,Y): return(XY) def DIFFERENCE(X,Y): return(X-Y)* Now lets save this file as AO.py then our module is created now.

Now we are creating our main python file: - import AO n1=int(input(‘Enter a Number’)) n2=int(input(‘Enter another Number’)) print(‘Sum = ‘, AO.ADD(n1,n2)) print(‘Difference=’,AO.DIFFERENCE(n1,n2)) print(‘Product=’,AO.PRODUCT(n1,n2))

Output: -

Q.9 Write a python code to find the size of the file in bytes, number of lines and number of words.

reading data from a file and find size, lines, words

f=open(‘Lines.txt’,’r’) str=f.read( ) size=len(str) print(‘size of file n bytes’,size) f.seek(0) L=f.readlines( ) word=L.split( ) print(‘Number of lines ’,len(L)) print(‘Number of words ’,len(word)) f.close( )

Q.10. What does stdin, stdout represent? Ans : stdin represent standard input device and stdout represent standard output device which are represented as files.

CHAPTER -6 RECURSION

Q.1 What is recursion in Python? Ans: Recursion is the process of defining something in terms of itself. We know that in Python, a function can call other functions. It is even possible for the function to call itself. These types of construct are termed as recursive functions. Q.2 What are the advantage and disadvantage of recursion? Ans: Advantages of Recursion:

  1. Recursive functions make the code look clean and elegant.
  2. A complex task can be broken down into simpler sub-problems using recursion.
  3. Sequence generation is easier with recursion than using some nested iteration. Disadvantages of Recursion:
  1. Sometimes the logic behind recursion is hard to follow through.
  2. Recursive calls are expensive (inefficient) as they take up a lot of memory and time.
  3. Recursive functions are hard to debug.

Q.3) Write recursive code to compute the factorial of an integer. Ans: def calc_factorial(x): if x == 1: return 1 else: return (x * calc_factorial(x-1)) num = 4 print("The factorial of", num, "is", calc_factorial(num))

Q.4) Write program to find the H.C.F of two input number Ans: def computeHCF(x, y): if x > y: smaller = y else: smaller = x

Chapter-7 Idea of Algorithmic Efficiency Q.1 What is an Algorithm? Ans: An Algorithm is a step by step process of solving any problem. Q.2 What do you mean by complexity of an algorithm? How it is measured? Ans: Complexity is a measure of efficiency of algorithm. It can be measured in terms of time required to execute an algorithm and/or memory space required for execution of an algorithm Q.3 What factors determine the complexity of an algorithm? Ans. The factors determining complexity of an algorithm are Time for execution and Memory space required during execution. Q4. How we determine time complexity of an algorithm? Ans: We determine the time complexity as the number of elementary instruction that it executes with respect to some given input size. Q.5 What are - best case & worst case? Ans: Best case means best possible performance (say minimum time required) of an algorithm. It indicates the optimal time of performance of any algorithm. Worst case means the worst possible performance (say maximum possible time required) of an algorithm. It provides an upper bound on running time. Example : for a binary search the best case for an input size of N is when it is sorted and the worst case is when it is unsorted. Q.6 What does Big-O notation indicate? Ans: Big-O notation indicates an algorithm’s growth rate which in turn determines the algorithm’s performance with increase in the input size. Q7. Give the meaning of O(1), O(N), O(log N) Ans: O(1) means an algorithms requires a constant time for its execution e.g. pushing an element at the top of a stack O(N) means an algorithm takes at most N time (or steps) for an input size of N e.g. searching an element in an array O(N^2 ) means an algorithm takes at most N^2 time (steps) for an input of size N e.g. Sorting an array using bubble sort

Q8. What is a Dominant term? Ans: As the input size grows it affects the efficiency of an algorithm. While describing the growth rate, the term that affect the most on algorithm’s performance is called dominant term. Example : if in an algorithm there are terms with growth rate O(N^2 ) and O(N) then the dominant term is O(N^2 ) Q9. Compare the efficiency of linear and binary search algorithms. Efficiency of binary search is better than that of linear search algorithm when the array is sorted as the growth rate of binary search is O(log N) which is much smaller than that of linear search O(N). Q10. State some external factors that may affect an algorithm’s efficiency. Ans: Size of input, Speed of the computer, Compiler that is used etc.

Q.5 Difference between Line and Bar Chart in Pyplot.. Ans. A line chart or line graph is a type of chart which displays information as a series of data points called 'markers' connected by straight line segments. Line graphs are usually used to find relationship between two data sets on different axis; for instance X, Y. A bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally.

CHAPTER -9 Data Structures : Linear Lists Q.1 What do you mean by Data Structure? Ans: Data Structure means organization of data. A data structure has well defined operations or behavior. Q2. How is Data Structure different from Data Type? Data Structure provides information regarding organization of data where as Data Type provides information regarding the domain of values and operations that can be perform on data Q3. Define - Stack, Queue, Tree. Stack – A stack is a linear list also known as LIFO list with the special property that items can be added or removed from only one end called the top. Queue – A queue is a linear list also known as FIFO list with the special property that items can be added added at one end and removed from the other. Tree – A non-linear hierarchical organization of data as nodes and links between them where the topmost node called root and bottom most nodes called leaves. Q4. Name some linear and non-linear data structures Linear Data Structures – Lists, Arrays, Stack, Queue Non Linear Data Structures – Trees, Graphs Q5. Name some operations commonly performed on data structures? Ans: Traversal, Insertion, Deletion, Searching, Sorting, Merging etc. Q6. What is a list? Ans: A list is a mutable sequence of data elements indexed by their position. A list is represented using [ ]. e.g L=[10,20,30] Q7. What is traversing? Write python code to traverse a list. Ans: Traversing means accessing or visiting or processing each element of any data structure. #List traversal L=[10,20,30,40,50] for x in L : print(x) Q8. Name the methods used for inserting and deleting elements from a list.