0% found this document useful (0 votes)
0 views4 pages

Class12 CS Questions With Answers

The document contains important questions and answers for Class 12 Computer Science covering topics such as flow of control, string manipulation, list manipulation, tuples, dictionaries, functions, exception handling, and file handling. Each section includes explanations, code examples, and predicted outputs for various programming scenarios. It serves as a study guide for students to understand key concepts and practice coding in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views4 pages

Class12 CS Questions With Answers

The document contains important questions and answers for Class 12 Computer Science covering topics such as flow of control, string manipulation, list manipulation, tuples, dictionaries, functions, exception handling, and file handling. Each section includes explanations, code examples, and predicted outputs for various programming scenarios. It serves as a study guide for students to understand key concepts and practice coding in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Class 12 Computer Science - Important Questions with Answers

1. Flow of Control
Q: What is the difference between 'if', 'if-else' and 'if-elif-else' statements?
A: 'if' is used for a single condition.
'if-else' is used when there are two options.
'if-elif-else' is used for multiple conditions.

Q: Predict output:
for i in range(3):
if i == 1:
continue
print(i)
A: Output:
0
2

Q: Write a program to check if a number is divisible by both 3 and 5.


A: num = int(input("Enter number: "))
if num % 3 == 0 and num % 5 == 0:
print("Divisible")
else:
print("Not Divisible")

Q: What is the role of break and continue in loops?


A: 'break' exits the loop early. 'continue' skips the current iteration and moves to the next.

2. String Manipulation
Q: What is the output of: 'Python'.upper()?
A: 'PYTHON'

Q: Write a program to count the number of vowels in a string.


A: s = input("Enter string: ")
vowels = 'aeiouAEIOU'
count = sum(1 for char in s if char in vowels)
print("Vowel count:", count)
Q: Predict output:
s = 'HelloWorld'
print(s[1:5])
A: Output:
elllo

Q: Differentiate between isdigit(), isalpha() and isalnum().


A: 'isdigit()' checks for digits, 'isalpha()' for alphabets, 'isalnum()' for alphanumeric characters.

3. List Manipulation
Q: Write a program to reverse a list without using reverse() function.
A: lst = [1, 2, 3, 4]
lst = lst[::-1]
print(lst)

Q: What is the difference between append() and insert()?


A: 'append()' adds to the end. 'insert(index, item)' adds item at specific index.

Q: Predict output:
list1 = [10, 20]
list1.append([30, 40])
print(list1)
A: Output:
[10, 20, [30, 40]]

Q: How do you remove an element from a list using pop()?


A: Use list.pop(index). Default is last element if index not given.

4. Working with Tuple


Q: What is the difference between list and tuple?
A: List is mutable (can change). Tuple is immutable (cannot change).

Q: Can we change values inside a tuple? Justify your answer.


A: No, tuples are immutable. You cannot modify their content.

Q: Predict output:
t = (1, 2, 3, 2)
print(t.count(2))
A: Output:
2

5. Working with Dictionary


Q: Write a program to count frequency of each word in a string.
A: s = input("Enter string: ")
words = s.split()
d = {}
for w in words:
d[w] = d.get(w, 0) + 1
print(d)

Q: How do you update a dictionary with a new key-value pair?


A: dict[key] = value

Q: Predict output:
d = {'a':1, 'b':2}; d['c']=3; print(d)
A: Output:
{'a':1, 'b':2, 'c':3}

Q: What is the difference between get() and direct access d[key]?


A: get() returns None if key not found. d[key] raises error if key missing.

6. Working with Functions


Q: Define a function to find the factorial of a number.
A: def factorial(n):
return 1 if n == 0 else n * factorial(n-1)

Q: What is the difference between parameters and arguments?


A: Parameters are defined in function header. Arguments are values passed when calling.

Q: Predict output:
def add(a,b): return a+b
print(add(2,3))
A: Output:
5

7. Exception Handling
Q: Write a short code to handle division by zero error.
A: try:
x = 1/0
except ZeroDivisionError:
print("Division by zero error")

Q: What is the purpose of try and except blocks?


A: They handle runtime errors without crashing the program.

8. File Handling (Text File)


Q: Write a Python program to read all lines of a text file.
A: f = open('file.txt', 'r')
print(f.read())
f.close()

Q: What is the difference between read(), readline() and readlines()?


A: read() -> whole file
readline() -> one line
readlines() -> list of lines

Q: How do you open a file in append mode and write to it?


A: f = open('file.txt', 'a')
f.write('Hello')
f.close()

Q: Predict output:
f = open('data.txt', 'w')
f.write('Hello')
f.close()
A: Output: File 'data.txt' is created and 'Hello' is written to it.

You might also like