0% found this document useful (0 votes)
7 views14 pages

1

The document contains a series of questions and answers related to computer science concepts, programming, and Python language. It covers topics such as hexadecimal digits, ASCII codes, problem-solving steps, algorithms, data types, and Python programming basics. Additionally, it includes examples of coding, operator types, and variable definitions.
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)
7 views14 pages

1

The document contains a series of questions and answers related to computer science concepts, programming, and Python language. It covers topics such as hexadecimal digits, ASCII codes, problem-solving steps, algorithms, data types, and Python programming basics. Additionally, it includes examples of coding, operator types, and variable definitions.
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/ 14

Q1. The hexadecimal digits are 1 to 0 and A to _______?

a) E b) F c) G d) D
Q2. ASCII is a _________ bit code and ISCII is a ___________ bit code.
a) 8, 7 b) 7, 8 c) 8, 8 d) 7, 7
Q3. According to the distributive law A (B+C) = __________.
a) ABC b) AB+AC c) A+B+C d) A+BC
Q4. Convert the following: (i)(4A)16=(______)2 (ii) (106)10=(_____)8
Q5. Convert the decimal number 106 to
a) binary and b) octal.
Q6. ASCII uses ____ bits to represent Characters.
a) 5 b) 6 c) 7 d) 8
Q7. How many bits forms a Kilo Byte?
a) 8194 Bits b) 8192 bits c) 4096 bits d) 1024 bits
Q8. Convert the following number in to given numbers:
a) (345.24)10 =( )2 b) (A35.57)16 =( )8
Q9. Convert the following numbers in the given equivalent number system.
a). (234.56)10 = ( )2 b) (1101111.11011)2 = ( )16
Q10. Full form of USB
a) Uniform Service Bus b) Universal Serial Bus c). Universal Sector Buffer d) Universal
Service Bus
Unit – 2 (Computational Thinking and Programming)
Problem and Problem Solving
In computer science, "problem" refers to a task or challenge that requires a solution. The process
of identifying a problem, developing an algorithm, and implementing an algorithm to develop a
computer program is called Problem Solving.
Steps required for solving a problem
● Analyzing the problem
● Developing an Algorithm
● Coding
● Testing and Debugging

Analyzing the Problem


This stage focuses on understanding the problem. If we do not have a clear understanding of the
problem, we may develop a computer program that cannot solve the problem correctly. In this
stage, we figure out the inputs, the outputs and the processing required to convert the input into
the output.
Developing Algorithm
This stage focuses on creating a logical sequence of instructions, called an Algorithm. An algorithm
has a distinct start and end point, as well as a defined number of steps.
Draw a flow-chart to identify whether a number taken as the input from the user is an
even number or an odd number?

Algorithm Flow Chart

21
START
Step 1 → Take an integer number A as
input Step 2 → Divide A by 2, and store the
remainder as r
Step 3 → If r is zero, Display 'Even'
Step 4 → Else Display 'Odd'
STOP

Coding
Coding is the process of creating computer programs.
Testing
Testing is a process to check if an application is working as expected. The main objective of
Testing is to find errors.
Debugging
Debugging is the activity to fix the errors found in the application during the testing phase.
Representation of Algorithms
There are two common methods of representing an algorithm —flowchart and pseudocode.

Pseudocode
● Pseudocode is a way of representing an algorithm in readable and easy language.
● Pseudocode is not an actual program. So, it cannot be executed.
● Some of the frequently used keywords while writing pseudocode are INPUT, COMPUTE, PRINT
IF/ELSE, START, STOP

Advantages of Pseudo-Code:
1. Easily convertible to a Programming Language
2. Easy to understand and read
Write a pseudocode for identifying if a number is even or odd.
INPUT number A
COMPUTE remainder as r = A%2
IF r ==0 PRINT 'Even'
ELSE PRINT 'Odd'

Decomposition
Decomposition is the process of breaking a complex computer problem into smaller parts that are
easily manageable and solvable

Computer Program:

22
A computer program is a set of instructions written in a programming language that can be
executed by the computer to perform and solve a certain task.
Python Programming Language:
Python is an interpreted, high-level programming language. It was developed by Guido van Rossum.
It is user-friendly and is most popular for its easy-to-use syntax and readable code. It is case
sensitive.

Working in Python
i) Install Python on the computer (https://www.python.org/downloads/).
ii) Use Python IDLE (Integrated Development and Learning Environment) for developing python
Programs.

How to Display Data


print( ) function is used to print a message on the screen.

A Simple Hello World Program


print("Hello World")

Modes of working in Python


● Interactive Mode
● Script Mode

Interactive Mode
In Interactive Mode, a python statement is executed in a command-line shell. It provides instant
feedback for each statement while keeping prior statements in memory.

Script Mode
In script mode, the instructions are saved in a file with a '.py' extension and executed from the
beginning of the file. This mode is suitable for creating and running large programs.

Character Set:
A character set is a collection of valid characters that can be recognized by a language. Python
Language recognises the following characters as valid:
23
Letters : A-Z, a-z
Digits : 0-9
Special Symbol : + / @ ! - = <> == etc.
Whitespaces : '\n', '\t' etc.
Tokens
Tokens are the smallest individual units of a program.

Keywords
Keywords are reserved words with special meaning (known to the interpreter). These can not be
used as identifiers in a program.
Example: for, while, else, if
Identifier
A variable, function, class, module, or other objects are identified by a name known as identifier.
Rules for naming an identifier:
1. First character of a variable can be an alphabet (A-Z or a-z) or an underscore(_).
2. Next characters of a variable can be an alphabet (A-Z or a-z), an underscore(_) or a digit.
3. Keywords cannot be used as variables.
4. First character cannot be a digit.
5. Special characters including white spaces are not allowed.

Literals:
Literals are data-items that have a fixed value of a certain data type, such as a number, string,
boolean, or None. They are also known as Constants.

Example :
String literals (Text) : 'Hello World'
Numeric literals (Numbers) : 3.14
Boolean literals (Truth Value) : True/False
None Literal : The None keyword is used to define a null value or absence of a value. None is
different from 0.

Operators and Operands


Operators are symbols (or keywords) that perform various operations on the operands. An operand
is a variable or a literal on which the operation is performed.
Example: 50 + 20
24
Here 50 and 20 are operands, and + is the operator.

Arithmetic Operators : Arithmetic operators perform mathematical operations like addition,


subtraction, multiplication, division, floor division, exponent and modulus. (+,-,*,/,//,**,%)

Relational Operators:
Relational operators perform comparison between values. An expression having relational operators
evaluates to either True or False. Example >, <, >=, <=, ==, !=

Logical Operators:
These operators are used to combine conditional statements.

Examples:

○ and: True if both the operands are true.


○ or: True if either of the operands is true.
○ not: True if the operand is false (complements the operand).

Assignment Operators: These operators are used to assign values to variables.

Examples:

○ = (Assign): Assigns the value on the right to the variable on the left.
○ +=, -=, *=, /=: These operators combine arithmetic operations with assignment.

Membership Operators:

These operators test for membership in sequences (lists, tuples, strings).

Examples:

○ in: True if value/variable is found in the sequence.


○ not in: True if value/variable is not found in the sequence.

Identity Operators:

These operators compare the memory locations of two objects.

Examples:

○ is: True if the operands are identical (refer to the same object).
○ is not: True if the operands are not identical (do not refer to the same object).

Python Comments
Comments are descriptions about the code. They help other programmers understand the
functionality of the code. Comments are ignored by the Python interpreter.

Variable

25
Variables refer to an object (data items like int, float, list, dictionary etc.) stored in the memory. Value
stored in a variable can be changed during the program execution.
Rules for naming a variable are the same as the rules for naming an Identifier.

Concept of L Value and R Value


In Python, the l-value refers to the left-hand side of an assignment operator, while the r-value refers
to the right-hand side of an assignment operator.

Consider the following assignment statement:


x = 5+2
In the statement above, the l-value is 'x' as it is on the left-hand side of the = operator.
The r-value is 7 as it is on the right-hand side of the = operator

● Knowledge of data types: Number(integer, floating point,complex), boolean, sequence(string, list,


tuple), None, Mapping(dictionary), mutable and immutable data types.

Data Type
Data type represents the type of data a Variable or a Literal is referring to. Each data type has specific
characteristics and operations associated with it. In Python, there are various data types, including
number, string, boolean, list, tuple, and dictionary.

Mutable and Immutable Data Objects


Python variables are memory references. It may be required to change or update the value of an
object referenced by a variable.

Mutable Objects:
Mutable data objects are objects that can be changed after they are created. It is possible to add,
remove, or modify elements within these data types. Example of mutable data types: List, Set and
Dictionary.
Immutable Objects :

26
Objects whose values cannot be changed after they are created are called immutable objects. To
change the value, a new object is created. Example of immutable data types: Number (Integer, Float),
String, and Tuple.

Numeric Data Types (Number)


Python has three numeric data types:
1. Integer
2. Float
3. Complex
Integer
● Numbers with No Fractional Part
● Can be Positive or Negative
● In Python 3, the int type has no max limit. Values can be as large as the available memory
allows.
● Example 100, 0o55 (octal number), 0x68(hexadecimal number)
Float
● Numbers with Fractional Part
● Example 3.14, .314E01
Complex
● Numbers with both real and imaginary components
● A complex number is represented by "x + yj". Example 10 + 9j
Boolean
A boolean data type can assume one of the two possible values : True or False.

Sequence: Sequence is an ordered collection of items or elements which includes several built-in
types as String, List, and Tuple. Values in the sequence are called elements/items. Each element in a
sequence has a unique index.
String:
● A string is an ordered sequence of characters enclosed in single/double/triple quotes.
● Single Line String : Terminates in a single line. Example – 'This is an example of single line'
● Multi Line String : Does not terminate in a single line. A multiline string may be created using
three quotes
List:
27
● List is an ordered sequence data type which can store values of any data type.
● List is mutable.
● List is enclosed in square brackets [ ]
● Example : [ ] is an empty List,
[5, 6.5, True, 'Hello'] is a List having 5, 6.5, True and 'Hello' as four elements.

Tuple:
● Tuple is an ordered sequence which can store values of any data type.
● They are immutable, i.e the items of a Tuple cannot be updated after creation.
● Tuple is enclosed in parenthesis ( )
● Example : t=( ) is an empty Tuple
t=(9,) is a Tuple with 9 as an item
t=(8, 5, 9.5, False) is a Tuple with 8, 5, 9.5, False as four items
Mapping
Mapping is an unordered data type in Python. Currently, there is only one standard mapping data
type in Python called dictionary.
Dictionary:
● Dictionary in Python stores items in the form of key-value pairs
● Syntax is dict_variable = {key1:value1, key2:value2, …, key-n:value-n}
● Items in a dictionary are enclosed in curly brackets { } and are separated by commas
● In a key-value pair, the key is separated from the value using a colon (:)
● To access any value in the dictionary, specify the key as the index using square brackets [ ].
● Example : { } is an empty dictionary,
D = {'name':'Python','version':'3.7.2', 'OS': 'Windows'}
print(D['version']) # shows 3.7.2 as output
Special Data-type: None
The None data type/keyword is used to indicate absence of a value (No value or Missing Value).
Example
>>> myVar = None
>>> print(type(myVar))
<class 'NoneType'>
>>> print(myVar)
None
Operator Precedence(PEMDAS)
PEMDAS stands for Parentheses, Exponents, Multiplication and Division, and Addition and
Subtraction. Let's look at each component:

28
Expression
An expression is combination of operators, operands, literals and parenthesis. An expression
produces a value when evaluated.
Evaluation of Expression
1. Evaluate the expression 50 + 20 * 30
Evaluation:
= 50 + (20 * 30) #precedence of * is more than that of +
= 50 + 600
= 650
2. Evaluate the expression 100 - 20 + 50
Evaluation:
The two operators (–) and (+) have equal precedence and the associativity is from left to right so the
left operator (i.e. -) will be evaluated first.
= (100 – 20) + 50
= 80 + 50
= 130
3. Evaluate the expression 9 + 3 ** 2 * 4 // 3
Evaluation:
= 9 + (3 ** 3) * 4 // 3
= 9 + 27 * 4 // 3 (* and // has left to right associativity)
= 9 + 108 // 3
= 9 + 36
= 45
Type conversion
It is the process of converting the value from one data type into another. Python supports two ways
of Type conversion:
● Implicit conversion
● Explicit conversion
Implicit conversion
This type of conversion is performed by Python Interpreter automatically without the user's
intervention.
Example:
num1 = 20 # num1 is integer
num2 = 30.5 # num2 is float
sum1 = num1 + num2 # sum1 will use float to avoid
# loss of fractional part during addition

29
print(sum1)
print(type(sum1))
Output:
50.5
<class 'float'>

Explicit Conversion:
This type of conversion is performed by the user manually. It is also known as type-casting. Explicit
type-casting is performed using functions such as int( ), float( ), str( ) etc.
Syntax : new_data_type (expression)
Example
num1 = input("Enter a number : ") # takes a string input by default
var1 = int(num1) #converts string to integer
var1 = var1 * 3
print(var)
Output
Enter a number : 2
6
Question 1:
Write the output of the given Python code :
a=0
a+ =2
print (a)
a) 2 b) 4 c) 6
Question 2:
Write the output of the given Python code :
a = 20
if a > = 22:
print(“if”)
elif a >= 21:
print(“elif”)
else:
print(“else”)
a) Else b) else c)elif
Question 3:
Which Operator is used for comparison of values?
a) Logical Operators b) Assignment Operators c) Relational Operators
Question 4:
What will be the output of the following code :
a=1
a, b = a+1, a+1
print(a)
print(b)
a) 2,2 b)4,2 c)2,6
Question 5:

30
Use IDLE to calculate : [CBSE Text Book]
(a) 6 + 4 * 10
(b) (6 + 4) * 10
i) (a) 46 b) 100 ii)a) 100 b)46 iii) a)100 b)100
Question 6:
What will be the output of the following code : [CBSE Text Book]
a = 3 – 4 + 10
b=5*6
c = 7.0/8.0
print(a,b,c, sep=”,”)
i)9,30,0 ii)9,30,1 iii)10,30,0
Question 7:
What will be the output of following code?
X, Y=2,6
X,Y = Y,X+2
print(X,Y)
a) 17 5 b)6 4 c)4 6
Question 8:
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x
print(x is z)
a) True b)False c)True or False
Question 9:
x = ["apple", "banana"]
print("banana" in x)
a) True b)False c)True or False
Question 10:
What will be the output of following code?
12 & 13
a) 12 b)13 c)12 & 13
Question 11:
Which are correct arithmetical operations?
i) a = 1*2
ii) 2 = 1+1
iii) 5 + 6 = y
iv) Seven = 3 * 4
Question 12:
Which operations result in 8?
i) 65 // 8
ii) 17 % 9
iii) 2 * * 4
iv) 64 * * 0.5
Question 13:
If the value of a = 20 and b = 20, then a+=b will assign ________ to a

31
a) 40 b) 30 c) 20 d) 10
Question 14:
The ____________ operator is used to find out if division of two number yields any remainder
a) / b) + c) % d) //
Question 15:
Which of the following is the relational operator
a. //
b. =
c. ==
d. and

Input and Output in Python


Data Input:
input( ) function is used for getting input from the user. It returns the input as a String data type by
default.
Explicit type casting is required to convert the input string into any other data type if required.
Data Output:
print( ) function is used for displaying output on the screen.
Example-1
num1 = int(input("Enter a Number : ")) # type casting the input from
String to Int
print("The Number is : ", num1)
Example-2
num = int(input("Enter a Number : "))
num_cube = num*num*num
print("The cube is : ",num_cube)

Errors
A programmer can make mistakes while writing a program, and hence, the program may not execute
or may generate wrong output. The process of identifying and removing such mistakes, also known
as bugs or errors, from a program is called debugging.
Errors occurring in programs can be categorised as:
i) Syntax errors.
ii) Logical errors
iii) Runtime errors
Syntax Error
Like other programming languages, Python has its own rules that determine its syntax. The
interpreter interprets the statements only if it is syntactically (as per the rules of Python) correct. If
any syntax error is present, the interpreter shows error message(s) and stops the execution there.
For example, parentheses must be in pairs, so the expression (10 + 12) is syntactically correct,
whereas (7 + 11 is not due to absence of right parenthesis. Such errors need to be removed before
the execution of the program
Some of the Common Syntax Errors are
● Parenthesis Mismatch
● Misspelled keyword

32
● Incorrect Indentation
Logical Error
Logical errors occur when the code runs without any errors, but the output is not as expected. Logical
errors are caused by a problem in the logic of the code.
Example : Average = mark_1 + mark_2 / 2 # incorrect calculation of average marks
Corrected Code : Average = (mark_1 + mark_2 ) / 2
Runtime Error
A runtime error causes abnormal termination of the program during the execution. Runtime error
occurs when the statement is correct syntactically, but the interpreter cannot execute it.
Runtime errors do not appear until after the program starts running or executing.
Example: 'division by zero'
num1 = 5.0
num2 = int(input("num2 = ")) #if the user inputs zero, a runtime error will occur
print(num1/num2)

Flow of Control
Flow of Control refers to the order in which statements are executed in a program.
Sequential Flow
The default control flow in a program is sequential flow, in which statements are executed line-by-
line one after the other in a sequence in which they are written.
Example
x=6
y=7
z=y-x
print(z)
Conditional Flow
Conditional flow refers to execution of certain statements only if a specific condition is met. This is
accomplished by the use of conditional statements such as if, if-else, and if-elif-else.
Example
num = int(input("Enter a number : "))
if(num>5):
print("Number is greater than 5")
else:
print("Number is less than 5")
Output
Enter a number : 55
Number is greater than 5
Iterative Flow
Iteration means 'repetition'.
Iterative flow repeats statements in a block of code. Repetition or looping can be performed a fixed
number of times or until a certain condition is met. This is accomplished through the use of iterative
statements: for and while.
Example-1
name = input("Enter your name : ")
for x in range(5): # range function creates a sequence of integers from 0 to 4

33
print("Hello", name)
Example-2
name = input("Enter your name : ")
i=1
while i<=5:
print("Hello", name)
i +=1
MCQ

1.Which one of the following is a valid Python if statement :


a. if a>=2 : b. if (a >= 2) c. if (a => 22) d. if a >= 22
2.The order of statement execution in the form of top to bottom is known as construct.
a. alternate b. sequence c. flow of data d. flow chart
3.The two membership operators are ..........and ............
a. in, not in b. true , false c.=,== d. none
4.A graphical representation of an algorithm to solve a problem is called ...............
a. flow of data b. barchart c. flow chart d. none
5.What is the logical expression for the following
Either A is greater than B or A is less than C
a. A>B or A<C b. A>B and A<C c . A>Band C d. A>Bor C
6.Which statement will check if a is equal to b?
a. if a = b: b. if a == b: c. if a === c: d. if a == b
7.Consider the given expression:
"Python" or True and "Programming" or not 70
Which of the following will be correct output if the given expression is evaluated ?
(a) True (b) False (c) ‘Python’ (d) ‘Programming’
8.What shape represents a decision in a flowchart ?
(a) A diamond (b) A rectangle (c) An oval (d) A parallelogram
9. To add and assign the value 10 to a variable a we cannot write
(a) a=a+10 (b) a+=10 (c ) a=+10 (d) a=10+10

Very Short Answers


Answer the Following Questions (Very Short Answers)
i. Define Algorithm
ii. What is decomposition?
iii. Why do we need Algorithm?
iv. What is meant by Debugging?
v. Write difference between algorithm and flowchart.
vi. Write the pseudocode to print all multiples of 5 between 10 and 25 (including both 10 and 25).
vii. Write an algorithm to find the greatest among two different numbers
viii. Write a pseudocode to calculate the factorial of a number
ix. Write an algorithm to find greatest among three numbers
x. Is ‘None’ and None same? Explain Why.
Case based:

34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy