← Back to Hub
Computer Science XII · NCERT Exercises

Question Bank

129 textbook exercises from all 13 chapters — grouped by chapter and exercise number.

Exercise 1

Q1Long
What is the difference between a syntax error and an exception? Justify with examples.
Q2Long
What will be the output of the following code?
try:
    num = int(input('Enter a number: '))  # User enters 'abc'
    print(num)
except ValueError:
    print('Invalid input!')
except:
    print('Some error occurred')
else:
    print('No error')
finally:
    print('Execution complete')
Q3Short
Fill in the blanks:
(i) The ______ statement is used to explicitly raise an exception.
(ii) The ______ block always executes whether or not an exception has occurred.
(iii) The ______ error is raised when we try to divide a number by zero.
(iv) The ______ block is executed only when no exception occurs in the try block.
Q4Long
Write a program that asks the user to enter a number and prints whether it is positive, negative or zero. Handle the ValueError exception if the user enters a non-numeric value.
Q5Short
Explain the use of the assert statement with an example.
Q6Short
What will be the output of the following code?
try:
    x = 10
    y = 0
    z = x / y
    print('Result:', z)
except ZeroDivisionError:
    print('Cannot divide by zero')
else:
    print('Division successful')
finally:
    print('Thank you')
Q7Long
Write a program that accepts two numbers from the user, divides the first number by the second, and handles the following exceptions:
(a) ZeroDivisionError — if the second number is 0
(b) ValueError — if the user enters a non-numeric value
Q8Short
What is the purpose of the finally clause? Why is it important in exception handling?
Q9Long
Write a program that opens a file 'data.txt' in read mode and handles the FileNotFoundError exception. Use the finally clause to display a message that the program has completed execution.

Exercise 2

Q1Short
Differentiate between text file and binary file.
Q2Long
What are the different file modes available in Python? Explain each.
Q3Long
Write a program to read a text file 'story.txt' line by line and display each word separated by a '#'.
Q4Short
What is the difference between write() and writelines() methods?
Q5Long
What is the difference between read(), readline() and readlines() methods?
Q6Short
What is the significance of the tell() and seek() methods?
Q7Long
What is pickling and unpickling? Explain with an example.
Q8Long
Write a program to count the number of lines in a text file 'poem.txt' that start with an uppercase letter.
Q9Long
Write a program using pickle to store and retrieve a list of employee records (each record is a dictionary with keys: empno, name, salary) in a binary file.
Q10Short
What is the advantage of using the 'with' statement for file handling?

Exercise 3

Q1Short
State True or False:
(a) Stack is a LIFO data structure.
(b) In a stack, elements can be added or removed from both ends.
(c) PUSH operation adds an element to the top of the stack.
(d) POP operation on an empty stack causes overflow.
(e) Postfix expressions require parentheses for correct evaluation.
Q2Long
Convert the following infix expressions to postfix:
(a) A + B - C
(b) (A + B) * (C - D)
(c) A * B + C / D
(d) ((A + B) * C - D) / E
(e) A + B * C ** D - E
Q3Short
Evaluate the following postfix expression: 5 3 + 8 2 - *
Q4Long
Write a Python program to implement a stack using a list with the following functions:
(a) push(stack, element) — to add an element
(b) pop(stack) — to remove and return the top element
(c) peek(stack) — to return the top element without removing
(d) display(stack) — to display all elements
(e) isEmpty(stack) — to check if the stack is empty
Q5Long
Write a Python program to check whether a given string is a palindrome using a stack.
Q6Short
What will be the output of the following code?
stack = []
stack.append(5)
stack.append(10)
stack.append(15)
print(stack.pop())
stack.append(20)
print(stack.pop())
print(stack.pop())
print(stack)
Q7Long
Write a Python program to implement a stack of student records. Each record contains roll number and name. Implement push, pop, and display operations using a menu-driven approach.

Exercise 4

Q1Short
Differentiate between a stack and a queue.
Q2Short
What is a deque? How is it different from a simple queue?
Q3Long
Write a Python program to implement a queue using a list with the following functions:
(a) enqueue(queue, element) — to add an element
(b) dequeue(queue) — to remove and return the front element
(c) peek(queue) — to return the front element without removing
(d) display(queue) — to display all elements
(e) isEmpty(queue) — to check if the queue is empty
Q4Short
What will be the output of the following code?
queue = []
queue.append(1)
queue.append(2)
queue.append(3)
print(queue.pop(0))
queue.append(4)
print(queue.pop(0))
print(queue)
Q5Long
Write a Python program to implement a deque using a list with functions to add and remove elements from both ends.
Q6Long
Write a Python program to implement a queue of customer orders in a restaurant. Each order has an order number and item name. Implement enqueue, dequeue, and display using a menu-driven approach.
Q7Short
List any four applications of queues in computer science.

Exercise 5.1

Q1Short
What is sorting? Why is it necessary?
Q2Long
Write the Bubble Sort algorithm and trace it for the list [5, 3, 8, 1, 2].
Q3Long
Write a Python function to implement Bubble Sort.
Q4Short
How many passes and comparisons does Bubble Sort need for a list of 6 elements in the worst case?
Q5Long
Explain Selection Sort with an example.
Q6Long
Write a Python function to implement Selection Sort.
Q7Long
Explain Insertion Sort with a suitable example.
Q8Long
Write a Python function to implement Insertion Sort.
Q9Long
Compare Bubble Sort, Selection Sort, and Insertion Sort in terms of time complexity, space complexity, and stability.
Q10Short
What is Big-O notation? List common time complexities in increasing order.

Exercise 6.1

Q1Short
What is searching? Explain the need for searching in computer science.
Q2Long
Write a Python function for Linear Search and trace it for the list [4, 9, 2, 7, 3] with search key 7.
Q3Long
Explain Binary Search algorithm with a suitable example.
Q4Long
Write a Python function for Binary Search (iterative version).
Q5Long
Compare Linear Search and Binary Search.
Q6Long
What is hashing? Explain hash function and collision.
Q7Short
What will be the maximum number of comparisons required to search for an element in a sorted list of 1024 elements using Binary Search?
Q8Short
Why can Binary Search not be applied to unsorted lists?
Q9Long
Write a recursive Python function for Binary Search.
Q10Long
A hash table of size 10 uses the hash function h(key) = key % 10. Insert the keys 31, 22, 43, 14, 52, 41 and show the hash table using linear probing.

Exercise 7.1

Q1Short
Differentiate between data and information with examples.
Q2Short
What is the difference between primary data and secondary data?
Q3Long
Calculate the mean, median, and mode for the data: 5, 8, 3, 7, 5, 9, 5, 2, 8, 6.
Q4Long
Calculate the variance and standard deviation for the data: 4, 8, 6, 5, 3.
Q5Short
What is meant by data cleaning? Why is it important?
Q6Short
Differentiate between variance and standard deviation.
Q7Short
When is median preferred over mean as a measure of central tendency?
Q8Long
What are the different types of data visualization? When should each be used?
Q9Long
Write a Python program to calculate mean, median, and mode of a list of numbers.
Q10Short
Explain the terms: structured data, unstructured data, and semi-structured data with examples.

Exercise 8.1

Q1Short
What is a database? Give examples.
Q2Long
What are the limitations of a file system that led to the development of DBMS?
Q3Long
Explain the terms: Relation, Attribute, Tuple, Domain, Degree, and Cardinality with reference to a relational database.
Q4Short
Differentiate between Candidate Key and Primary Key.
Q5Long
What is a Foreign Key? Explain with an example.
Q6Short
A relation EMPLOYEE has attributes EmpNo, AadhaarNo, Name, Dept, and Salary. Identify candidate keys, primary key, and alternate key.
Q7Long
What are the advantages of DBMS over the file system?
Q8Short
What is referential integrity? Why is it important?
Q9Short
What is a composite key? Give an example.
Q10Short
Explain the three levels of data abstraction.

Exercise 9.1

Q1Short
What is SQL? What are its advantages?
Q2Short
Differentiate between DDL and DML commands with examples.
Q3Short
Differentiate between CHAR and VARCHAR data types.
Q4Long
Write SQL commands to: (a) Create a database named SCHOOL. (b) Create a table STUDENT with columns RollNo (int, primary key), Name (varchar 30, not null), Class (varchar 5), Marks (float, default 0). (c) Display the structure of the table.
Q5Long
Write SQL commands to insert the following data into the STUDENT table: (1, 'Aman', 'XII', 85), (2, 'Priya', 'XII', 92), (3, 'Rahul', 'XI', 78), (4, 'Sneha', 'XII', 88), (5, 'Karan', 'XI', 65).
Q6Long
Write SQL queries for the following based on the STUDENT table: (a) Display all records. (b) Display names of students in class XII. (c) Display students with marks greater than 80. (d) Display students in class XII with marks between 80 and 95.
Q7Long
Write SQL queries using LIKE, IN, ORDER BY, and DISTINCT on the STUDENT table.
Q8Long
Write SQL queries to: (a) Count total students. (b) Find average marks. (c) Find highest and lowest marks. (d) Find total marks of all students.
Q9Long
Write SQL queries using GROUP BY and HAVING: (a) Count students in each class. (b) Find average marks per class. (c) Display classes where average marks exceed 80.
Q10Long
Write SQL commands to: (a) Update marks of RollNo 5 to 70. (b) Add 5 marks to all students in class XI. (c) Delete the record of RollNo 3. (d) Add a new column Email (VARCHAR 40) to the table.
Q11Short
Explain the difference between WHERE and HAVING clauses.
Q12Long
Write SQL queries using string functions: (a) Display names in uppercase. (b) Display first 3 characters of each name. (c) Display the length of each name. (d) Display names and their reverse.
Q13Long
Consider two tables: STUDENT(RollNo, Name, Class) and RESULT(RollNo, Subject, Marks). Write SQL queries to: (a) Display the Cartesian product. (b) Display names and marks using equi-join. (c) Display names of students who scored more than 90 in any subject.
Q14Short
What is the difference between UNION and Cartesian Product?
Q15Short
What is a sub-query? Write an SQL query to find the name of the student with the highest marks using a sub-query.
Q16Short
Write the general syntax and order of clauses in a SELECT statement.

Exercise 10.1

Q1Short
What is a computer network?
Q2Long
What are the advantages of computer networks?
Q3Short
What was ARPANET? When was it developed?
Q4Long
Differentiate between LAN, MAN, and WAN.
Q5Short
What is the difference between a hub and a switch?
Q6Short
What is the difference between a switch and a router?
Q7Long
Compare bus, star, and mesh topologies.
Q8Short
What is the difference between IPv4 and IPv6?
Q9Short
What is DNS? How does it work?
Q10Short
What is IoT? Give examples.
Q11Short
What is the difference between MAC address and IP address?
Q12Short
What is a modem? Explain its working.

Exercise 11.1

Q1Short
What are the basic components of data communication?
Q2Long
Differentiate between simplex, half-duplex, and full-duplex modes of communication.
Q3Short
What is bandwidth? How is data transfer rate measured?
Q4Long
Differentiate between circuit switching and packet switching.
Q5Long
Compare twisted pair cable, coaxial cable, and fiber optic cable.
Q6Long
What are radio waves, microwaves, and infrared waves? Compare them.
Q7Long
Explain the evolution of mobile telecommunication from 1G to 5G.
Q8Short
What is the difference between HTTP and HTTPS?
Q9Short
What is the difference between SMTP and POP3?
Q10Short
What is VoIP?
Q11Short
How long will it take to transfer a 2 MB file over a 10 Mbps connection?

Exercise 12.1

Q1Short
What is malware? List the different types of malware.
Q2Long
What is the difference between a virus, worm, and trojan?
Q3Short
What is phishing? How can you prevent it?
Q4Short
Differentiate between HTTP and HTTPS.
Q5Long
What is a firewall? Explain the difference between hardware and software firewalls.
Q6Short
What are cookies? What are the different types?
Q7Short
Differentiate between hackers and crackers.
Q8Short
What is a Denial of Service (DoS) attack?
Q9Short
What is the difference between phishing and pharming?
Q10Short
What is a Man-in-the-Middle attack? How can it be prevented?
Q11Short
What is ransomware? Give an example.

Exercise 13.1

Q1Short
What is Project Based Learning? What are its benefits?
Q2Long
List and explain the steps involved in developing a software project.
Q3Short
Why is teamwork important in software development?
Q4Short
What is version control? Why is it useful?
Q5Long
Describe the features of a Library Management System project.
Q6Short
What are CRUD operations? Why are they important in project development?