Computer Science - Informatics Practices Class XI/XII Python List Solved Test Part 7

 

Computer Science - Informatics Practices Class XI/XII Python List Solved Test  Part 7

Q1. Write the role of reverse() function in list.

Q2. reverse() function create new list. (T/F)

Q3. >>> a = [1,2,3,4]

    >>> a.reverse()

    >>> print(a)

Write the output of above code.

Q4. What type of error return by index() function,

    if element is not present in list?

Q5. a = [1,5,7,5]

    b = a.index(5)

    print(b)

Write the output of above code.

Q6. Write the output of the following:

Code

Output

 

 

 

 

Q7. len() function returns the ______ of the list.

Q8. By default sort() function arrange the 

    elements in ________ order  

    (increasing/decreasing)

Q9. Write code to arrange elements of following 

    list in increasing order.

    A = [23,12,45,32,67,33]

Q10. What do you mean by nested list. Give one

     example of nested list.


  

SOLUTIONS

Q1. Write the role of reverse() function in list.

Ans reverse() function arrange the elements of 

    list in the reverse order.

Q2. reverse() function create new list. (T/F)

Ans. False

Q3. >>> a = [1,2,3,4]

    >>> a.reverse()

    >>> print(a)

Write the output of above code.

Ans. [4,3,2,1]

Q4. What type of error return by index() function,

    if element is not present in list?

Ans. Index error

Q5. a = [1,5,7,5]

    b = a.index(5)

    print(b)

Write the output of above code.

Ans. 1

Q6. Write the output of the following:

Code

Output

 Indexerror

 [1,'a',3,4]

 [4,5,6,7]

 

Q7. len() function returns the ______ of the list.

Ans length

Q8. By default sort() function arrange the 

    elements in ________ order  

    (increasing/decreasing)

Ans. increasing

Q9. Write code to arrange elements of following 

    list in increasing order.

    A = [23,12,45,32,67,33]

Ans. A.sort()

Q10. What do you mean by nested list. Give one

     example of nested list.

Ans. A list inside another list is called nested list. for example

A = [1,2,3,['a','b']]

 

 

 

 

Computer Science - Informatics Practices Class XI/XII Python List Solved Test Part 6

 

Computer Science - Informatics Practices Class XI/XII Python List Solved Test  Part 6

Q1. Write a program to create list of the

    following (take input from the user)

a.     Any five students name

b.     Any five numbers

c.     Any five alphabets

d.     Any five name of colors.

Q2. Write a program to accept 10 numbers from the user, if the number is odd, and then add that number to the list.

(input numbers are : 1,2,3,4,5,6,7,8,9,10

L = [1, 3, 5, 7, 9])

Q3. Write a program to find the largest number from the following list.(without using inbuilt function)

A = [23, 12, 45, 67, 55]

Q4. Write a program to find the second largest number from the following list.

A = [23, 12, 45, 67, 55]

Q5. Explain the extend() function with example.

Q6. Write any one difference between insert() and append() function.


SOLUTIONS

Q1. Write a program to create list of the

    following (take input from the user)

a.     Any five students name

b.     Any five numbers

c.     Any five alphabets

d.     Any five name of colors.


Ans a)

a = []
for i in range(5):
nm=input("Enter student name")
a.append(nm)
print(a)

b)
a = []
for i in range(5):
num=int(input("Enter any number"))
a.append(num)
print(a)

c)
a = []
for i in range(5):
al=input("Enter any alphabet")
a.append(al)
print(a)

d)

a = []
for i in range(5):
cl=input("Enter name of any color")
a.append(cl)
print(a)

Q2. Write a program to accept 10 numbers from the user, if the number is odd, and then add that number to the list.

(input numbers are : 1,2,3,4,5,6,7,8,9,10

L = [1, 3, 5, 7, 9])

Ans.

a = []
for i in range(10):
num=int(input("Enter any number"))
if(num%2!=0):
a.append(num)
print(a)

Q3. Write a program to find the largest number from the following list.(without using inbuilt function)

A = [23, 12, 45, 67, 55]

Ans

A = [23, 12, 45, 67, 55]
max=A[0]
for i in range(len(A)):
if A[i]>max:
max=A[i]
print(max)

Q4. Write a program to find the second largest number from the following list.

A = [23, 12, 45, 67, 55]

Ans.

A = [23, 12, 45, 67, 55]
A.sort()
print(A[-2])

Q5. Explain the extend() function with example.

Ans. extend() function add complete list or specified elements of list at the end of another list. for example

A = [23, 12, 45, 67, 55]
B = ["Amit",2,3]
A.extend(B)
print(A)

OUTPUT : [23, 12, 45, 67, 55, 'Amit', 2, 3]

Q6. Write any one difference between insert() and append() function.

Ans. insert() function add elements at the specified index of list while append() function add the elements at the end of the list.

Computer Science - Informatics Practices Class XI/XII Python List Solved Test Part 5

 

Computer Science - Informatics Practices Class XI/XII Python List Solved Test  Part 5

Q1. Index value of first element in list is ___

Q2. Index value of last element in list is ____

Q3. Write one difference between indexing and

    slicing.

Q4. The syntax of slicing is:

       List[start : stop : step]

    Which argument is optional out of start,

    stop and step?

Q5. Write the output of the following:

            b = "Informatics"
            a=list(b)
            1.          print(a)
            2.          print(len(a))
            3.          print(a[1:4])
            4.          print(a[1:7])
            5.          print(a[3:])
            6.          print(a[:5])
            7.          print(a[4:17])
            8.          print(a[-2:-5:-1])
            9.          print(a[1:7:1])
            10.       print(a[1:7:2])
            11.       print(a[-5:])
            12.       print(a[:4])
            13.       print(a[-2:-5:-2])
            14.       print(a[11:15])
            15.       print(a[:])
            16.       print(a[::2])
            17.       print(a[-5:-1])
            18.       print(a[7:1:-1])
            19.       print(a[3:-3])
            20.       print(a[30:40])
            21.       print(a[17:-1])
            22.       print(a[10::-1])

 

Q6. Write a code to make copy of following list using copy function.

       a = [1,2,3,4]

Q7. What do you mean by append() function?

Q8. Write the output of the following:

code

output

>>> a = [1,2,3,4]

>>> a.append(7)

>>> a

 

>>> a = [1,2,3,4]

>>> a.append([7,8])

>>> a

 

Solutions

Ans 1 : 0

Ans 2 : -1

Ans 3 : By indexing we can extract only one element of list while by slicing we can fetch a sub list from main list.

Ans 4 : Step

Ans 5 :

  1)          ['I', 'n', 'f', 'o', 'r', 'm', 'a', 't', 'i', 'c', 's']
2)          11
3)          ['n', 'f', 'o']
4)          ['n', 'f', 'o', 'r', 'm', 'a']
5)          ['o', 'r', 'm', 'a', 't', 'i', 'c', 's']
6)          ['I', 'n', 'f', 'o', 'r']
7)          ['r', 'm', 'a', 't', 'i', 'c', 's']
8)          ['c', 'i', 't']
9)          ['n', 'f', 'o', 'r', 'm', 'a']
10)       ['n', 'o', 'm']
11)       ['a', 't', 'i', 'c', 's']
12)       ['I', 'n', 'f', 'o']
13)       ['c', 't']
14)       []
15)       ['I', 'n', 'f', 'o', 'r', 'm', 'a', 't', 'i', 'c', 's']
16)       ['I', 'f', 'r', 'a', 'i', 's']
17)       ['a', 't', 'i', 'c']
18)       ['t', 'a', 'm', 'r', 'o', 'f']
19)       ['o', 'r', 'm', 'a', 't']
20)       []
21)       []
22)       ['s', 'c', 'i', 't', 'a', 'm', 'r', 'o', 'f', 'n', 'I']

Ans 6.

>>> import copy

>>> a = [1,2,3,4]

>>> b=copy.copy(a)

Ans 7. Append function add one element or a list at the end of the list.

Ans 8.

code

output

>>> a = [1,2,3,4]

>>> a.append(7)

>>> a

[1,2,3,4,7]

>>> a = [1,2,3,4]

>>> a.append([7,8])

>>> a

[1,2,3,4,[7,8]]

 

Most Recently Published

File Handling Test 6

 File Handling Test 6 Q1 . Do we need to use the close() function when we use 'with' statement to open a file? Q2. Which mode you wi...

CS - IP Assignment/Worksheet