Sumita Arora
Sumita Arora
com/learn/sumita-arora-python-computer-
science-class-12-cbse/solutions/Y3E4M/revision-tour-1
Question 1
What are tokens in Python? How many types of tokens are allowed in Python?
Exemplify your answer.
The smallest individual unit in a program is known as a Token. Python has following
tokens:
Question 2
Question 3
What are literals in Python? How many types of literals are allowed in Python?
Literals are data items that have a fixed value. The different types of literals allowed
in Python are:
1. String literals
2. Numeric literals
3. Boolean literals
4. Special literal None
5. Literal collections
Question 4
Question 5
Out of the following, find those identifiers, which cannot be used for naming
Variables or Functions in a Python program:
Price*Qty
class
For
do
4thCol
totally
Row31
_Amount
Answer
Question 6
1. MyName
2. True
3. 2ndName
4. My_Name
The invalid identifier are:
Question 7
1. Set
2. None
3. Integer
4. Real
Real
Reason — Real is not a standard built-in data type in Python.
Question 8
1. ?
2. <
3. **
4. and
**
Reason — Let's go through each option and see if its valid arithmetic operator or
not:
Question 9
How are floating constants represented in Python? Give examples to support your
answer.
Answer
Floating constants are represented in Python in two forms — Fractional Form and
Exponent form. Examples:
Question 10
Question 11
What are operators ? What is their function? Give examples of some unary and
binary operators.
Operators are tokens that trigger some computation/action when applied to variables
and other objects in an expression. Unary plus (+), Unary minus (-), Bitwise
complement (~), Logical negation (not) are a few examples of unary operators.
Examples of binary operators are Addition (+), Subtraction (-), Multiplication (*),
Division (/).
Question 12
1. **
2. */
3. like
4. ||
5. is
6. ^
7. between
8. in
** ⇒ Exponentiation operator
is ⇒ Identity operator
^ ⇒ Bitwise XOR operator
in ⇒ Membership operator
Question 13
What is an expression and a statement?
An expression is any legal combination of symbols that represents a value. For
example, 2.9, a + 5, (3 + 5) / 4.
A statement is a programming instruction that does something i.e. some action takes
place. For example:
print("Hello")
a = 15
b = a - 10
Question 14
Question 15
Question 16
1. True
2. False
3. None
4. Null
True
Reason — The 'not' operator has the highest precedence, followed by 'and', which
has precedence over 'or', and the evaluation proceeds from left to right.
not True and False or True
= False and False or True
= False or True
= True
Question 17
Describe the concepts of block and body. What is indentation and how is it related to
block and body?
A block in Python, represents a group of statements executed as a single unit.
Python uses indentation to create blocks of code. Statements at same indentation
level are part of same block/suite and constitute the body of the block.
Question 18(a)
Question 18(b)
1. Integer
2. String
3. List
4. Tuple
Question 19
1. Integers (signed)
2. Booleans
Question 20
What are immutable and mutable types? List immutable and mutable types of
Python.
Mutable types are those whose values can be changed in place whereas Immutable
types are those that can never change their value in place.
Mutable types in Python are:
1. Lists
2. Dictionaries
3. Sets
1. Integers
2. Floating-Point numbers
3. Booleans
4. Strings
5. Tuples
Question 21
What is the difference between implicit type conversion and explicit type conversion?
Example: Example:
a, b = 5, 25.5 a, b = 5, 25.5
c=a+b c = int(a + b)
Question 22
An immutable data type is one that cannot change after being created. Give three
reasons to use immutable data.
Three reasons to use immutable data types are:
1. Immutable data types increase the efficiency of the program as they are
quicker to access than mutable data types.
2. Immutable data types helps in efficient use of memory storage as different
variables containing the same value can point to the same memory location.
Immutability guarantees that contents of the memory location will not change.
3. Immutable data types are thread-safe so they make it easier to parallelize the
program through multi-threading.
Question 23
What is entry controlled loop? Which loop is entry controlled loop in Python?
An entry-controlled loop checks the condition at the time of entry. Only if the
condition is true, the program control enters the body of the loop. In Python, for and
while loops are entry-controlled loops.
Question 24
Question 25
Rewrite the adjacent code in python after removing all syntax error(s). Underline
each correction done in the code.
30 = To
for K in range(0,To)
IF k%4 == 0:
print(K * 4)
Else:
print(K+3).
Explanation
Correction 1 — Variable should be on left side and literals should be on right side.
Correction 2 — Semi-colon was missing in the for loop syntax.
Correction 3 — if statement should be in lower case.
Correction 4 — else statement should be in lower case.
Correction 5 — Full stop should not be there at the end of print function.
Question 26
Below are seven segments of code, each with a part coloured. Indicate the data type
of each coloured part by choosing the correct type of data from the following type.
(a) int
(b) float
(c) bool
(d) str
(e) function
(f) list of int
(g) list of str
(i)
if temp < 32 :
print ("Freezing")
(ii)
L = ['Hiya', 'Zoya', 'Preet']
print(L[1])
(iii)
M = []
for i in range (3) :
M.append(i)
print(M)
(iv)
L = ['Hiya', 'Zoya', 'Preet']
n = len(L)
if 'Donald' in L[1 : n] :
print(L)
(v)
if n % 2 == 0 :
print("Freezing")
(vi)
L = inputline.split()
while L != ( ) :
print(L)
L = L[1 :]
(vii)
L = ['Hiya', 'Zoya', 'Preet']
print(L[0] + L[1])
Answer
(i) bool
(ii) str
(iii) list of int
(iv) int
(v) bool
(vi) list of str
(vii) str
Question 27
Answer
Output
$$
$$$$
$$$$$$
Explanation
Question 1
Fill in the missing lines of code in the following code. The code reads in a limit
amount and a list of prices and prints the largest price that is less than the limit. You
can assume that all prices and the limit are positive numbers. When a price 0 is
entered the program terminates and prints the largest price that is less than the limit.
#Read the limit
limit = float(input("Enter the limit"))
max_price = 0
# Read the next price
next_price = float(input("Enter a price or 0 to stop:"))
while next_price > 0 :
<write your code here>
#Read the next price
<write your code here>
if max_price > 0:
<write your code here>
else :
<write your code here>
Answer
#Read the limit
limit = float(input("Enter the limit"))
max_price = 0
# Read the next price
next_price = float(input("Enter a price or 0 to stop:"))
while next_price > 0 :
if next_price < limit and next_price > max_price:
max_price = next_price
#Read the next price
next_price = float(input("Enter a price or 0 to stop:"))
if max_price > 0:
print("Largest Price =", max_price)
else :
print("Prices exceed limit of", limit);
Question 2a
Output
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Question 2b
Output
10 0
9 1
8 2
7 3
6 4
Explanation
x y Output Remarks
10 0 10 0 1st Iteration
10 0
9 1 2nd Iteration
91
10 0
8 2 91 3rd Iteration
82
7 3 10 0 4th Iteration
91
x y Output Remarks
82
73
10 0
91
6 4 82 5th Iteration
73
64
Question 2c
Output
100
90
80
70
60
50
Explanation
Question 2d
Question 2e
Output
1
2
3
4
5
Explanation
x will be assigned each of the values from the list one by one and that will get
printed.
Question 2f
Output
1
2
3
4
5
6
7
8
9
Explanation
will generate a sequence like this [1, 2, 3, 4, 4, 5, 6, 7, 8, 9]. p will be
range(1,10)
assigned each of the values from this sequence one by one and that will get printed.
Question 2g
Output
-500
-400
-300
-200
-100
0
100
200
300
400
Explanation
Question 2h
Explanation
⇒ 10 - 5 * 2
x-y*2
Question 2i
Output
50
Explanation
Outer loop executes 10 times. For each iteration of outer loop, inner loop executes 5
times. Thus, the statement c += 1 is executed 10 * 5 = 50 times. c is incremented by
1 in each execution so final value of c becomes 50.
Question 2j
Output
%
*
* *
* * *
%%
*
* *
* * *
%%%
*
* *
* * *
Explanation
In this code, the for loop is nested inside the while loop. Outer while loop runs 3
times and prints % as per the elements in x in each iteration. For each iteration of
while loop, the inner for loop executes 3 times printing * as per the elements in x.
Question 2k
Output
L
A
M
P
Explanation
The for loop extracts each letter of the string 'lamp' one by one and place it in
variable x. Inside the loop, x is converted to uppercase and printed.
Question 2l
Output
o t
n w
e o
Explanation
Inside the while loop, each letter of x and y is accessed one by one and printed.
Question 2m
Predict the output of the following code fragments:
x = "apple, pear, peach"
y = x.split(", ")
for z in y :
print(z)
Answer
Output
apple
pear
peach
Explanation
x.split(", ") breaks up string x into a list of strings so y becomes ['apple', 'pear',
'peach']. The for loop iterates over this list and prints each string one by one.
Question 2n
Output
apple
PEAR
PEACH
grapefruit
Explanation
x.split(', ') breaks up string x into a list of strings so y becomes ['apple', 'pear',
'peach', 'grapefruit']. The for loop iterates over this list. apple and grapefruit are less
than m (since a and g comes before m) so they are converted to lowercase and
printed whereas pear and peach are converted to uppercase and printed.
Question 3
Which of the following is the correct output for the execution of the following Python
statement ?
print(5 + 3 ** 2 / 2)
1. 32
2. 8.0
3. 9.5
4. 32.0
Answer
9.5
Explanation
Question 4(i)
How many times will the following for loop execute and what's the output?
for i in range(-1, 7, -2):
for j in range (3):
print(1, j)
Answer
The loops execute 0 times and the code produces no output. range(-1, 7, -2) returns
an empty sequence as there are no numbers that start at -1 and go till 6
decrementing by -2. Due to empty sequence, the loops don't execute.
Question 4(ii)
How many times will the following for loop execute and what's the output?
for i in range(1,3,1):
for j in range(i+1):
print('*')
Answer
Loop executes for 5 times.
Output
*
*
*
*
*
Explanation
range(1,3,1) returns [1, 2]. For first iteration of outer loop j is in range [0, 1] so inner
loop executes twice. For second iteration of outer loop j is in range [0, 1, 2] so inner
loop executes 3 times. This makes the total number of loop executions as 2 + 3 = 5.
Question 5
Output
Jay
Finished!
Riya
Finished!
Tanu
Got it!
Explanation
The for loop iterates over each name in the list and prints it. If the name does not
begin with the letter T, Finished! is printed after the name. If the name begins with T,
break statement is executed that terminates the loop. Outside the loop, Got it! gets
printed.
Question 6
Is the loop in the code below infinite? How do you know (for sure) before you run it?
m = 3
n = 5
while n < 10:
m = n - 1
n = 2 * n - m
print(n, m)
Answer
The loop is not infinite. To know this without running it we can analyze how n is
changed inside the loop in the following way:
n=2*n-m
Substituting value of m from m = n - 1,
⇒n=2*n-n+1
n = 2 * n - (n - 1)
⇒ n = 2n - n + 1
⇒n=n+1
Therefore, inside the loop n is incremented by 1 in each iteration. Loop condition is n
< 10 and initial value of n is 5. So after 5 iterations, n will become 10 and the loop
will terminate.
Type C: Programming Practice/Knowledge based Questions
Question 1
Write a program to print one of the words negative, zero, or positive, according to
whether variable x is less than zero, zero, or greater than zero, respectively.
Solution
x = int(input("Enter x: "))
if x < 0:
print("negative")
elif x > 0:
print("positive")
else:
print("zero")
Output
Enter x: -5
negative
Enter x: 0
zero
Enter x: 5
positive
Question 2
Write a program that returns True if the input number is an even number, False
otherwise.
Solution
x = int(input("Enter a number: "))
if x % 2 == 0:
print("True")
else:
print("False")
Output
Enter a number: 10
True
Enter a number: 5
False
Question 3
Write a Python program that calculates and prints the number of seconds in a year.
Solution
days = 365
hours = 24
mins = 60
secs = 60
secsInYear = days * hours * mins * secs
print("Number of seconds in a year =", secsInYear)
Output
Number of seconds in a year = 31536000
Question 4
Write a Python program that accepts two integers from the user and prints a
message saying if first number is divisible by second number or if it is not.
Solution
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a % b == 0:
print(a, "is divisible by", b)
else:
print(a, "is not divisible by", b)
Output
Enter first number: 15
Enter second number: 5
15 is divisible by 5
Question 5
Write a program that asks the user the day number in a year in the range 2 to 365
and asks the first day of the year — Sunday or Monday or Tuesday etc. Then the
program should display the day on the day-number that has been input.
Solution
dayNames = ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY",
"SATURDAY", "SUNDAY"]
if currDayIdx >= 7:
currDayIdx = currDayIdx - 7
Output
Enter day number: 243
First day of year: FRIDAY
Day on day number 243 : TUESDAY
Question 6
One foot equals 12 inches. Write a function that accepts a length written in feet as an
argument and returns this length written in inches. Write a second function that asks
the user for a number of feet and returns this value. Write a third function that
accepts a number of inches and displays this to the screen. Use these three
functions to write a program that asks the user for a number of feet and tells them
the corresponding number of inches.
Solution
def feetToInches(lenFeet):
lenInch = lenFeet * 12
return lenInch
def getInput():
len = int(input("Enter length in feet: "))
return len
def displayLength(l):
print("Length in inches =", l)
ipLen = getInput()
inchLen = feetToInches(ipLen)
displayLength(inchLen)
Output
Enter length in feet: 15
Length in inches = 180
Question 7
Write a program that reads an integer N from the keyboard computes and displays
the sum of the numbers from N to (2 * N) if N is nonnegative. If N is a negative
number, then it's the sum of the numbers from (2 * N) to N. The starting and ending
points are included in the sum.
Solution
n = int(input("Enter N: "))
sum = 0
if n < 0:
for i in range(2 * n, n + 1):
sum += i
else:
for i in range(n, 2 * n + 1):
sum += i
Output
Enter N: 5
Sum = 45
Enter N: -5
Sum = -45
Question 8
Write a program that reads a date as an integer in the format MMDDYYYY. The
program will call a function that prints print out the date in the format <Month Name>
<day>, <year>.
Sample run :
Enter date : 12252019
December 25, 2019
Solution
months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]
Output
Enter date in MMDDYYYY format: 12252019
December 25, 2019
Question 9
Write a program that prints a table on two columns — table that helps converting
miles into kilometers.
Solution
print('Miles | Kilometres')
print(1, "\t", 1.60934)
for i in range(10, 101, 10):
print(i, "\t", i * 1.60934)
Output
Miles | Kilometres
1 1.60934
10 16.0934
20 32.1868
30 48.2802
40 64.3736
50 80.467
60 96.5604
70 112.6538
80 128.7472
90 144.8406
100 160.934
Question 10
Write another program printing a table with two columns that helps convert pounds in
kilograms.
Solution
print('Pounds | Kilograms')
print(1, "\t", 0.4535)
for i in range(10, 101, 10):
print(i, "\t", i * 0.4535)
Output
Pounds | Kilograms
1 0.4535
10 4.535
20 9.07
30 13.605
40 18.14
50 22.675
60 27.21
70 31.745
80 36.28
90 40.815
100 45.35
Question 11
Write a program that reads two times in military format (0900, 1730) and prints the
number of hours and minutes between the two times.
A sample run is being given below :
Please enter the first time : 0900
Please enter the second time : 1730
8 hours 30 minutes
Solution
ft = input("Please enter the first time : ")
st = input("Please enter the second time : ")
Output
Please enter the first time : 0900
Please enter the second time : 1730
8 hours 30 minutes
Assignments
Question 1
Write a query to display EName and Sal of employees whose salary is greater than
or equal to 2200 from table Empl.
Answer
SELECT ENAME, SAL
FROM empl
WHERE SAL >= 2200;
Output
+-----------+------+
| ENAME | SAL |
+-----------+------+
| MAHADEVAN | 2985 |
| BINA | 2850 |
| AMIR | 5000 |
| SHIAVNSH | 2450 |
| SCOTT | 3000 |
| FAKIR | 3000 |
+-----------+------+
Question 2
Write a query to display details of employees who are not getting commission from
table Empl.
Answer
SELECT *
FROM empl
WHERE COMM IS NULL OR COMM = 0;
Output
+-------+-----------+-----------+------+------------+------+------+--------
+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO
|
+-------+-----------+-----------+------+------------+------+------+--------
+
| 8369 | SMITH | CLERK | 8902 | 1990-12-18 | 800 | NULL | 20
|
| 8566 | MAHADEVAN | MANAGER | 8839 | 1991-04-02 | 2985 | NULL | 20
|
| 8698 | BINA | MANAGER | 8839 | 1991-05-01 | 2850 | NULL | 30
|
| 8839 | AMIR | PRESIDENT | NULL | 1991-11-18 | 5000 | NULL | 10
|
| 8844 | KULDEEP | SALESMAN | 8698 | 1991-09-08 | 1500 | 0 | 30
|
| 8882 | SHIAVNSH | MANAGER | 8839 | 1991-06-09 | 2450 | NULL | 10
|
| 8886 | ANOOP | CLERK | 8888 | 1993-01-12 | 1100 | NULL | 20
|
| 8888 | SCOTT | ANALYST | 8566 | 1992-12-09 | 3000 | NULL | 20
|
| 8900 | JATIN | CLERK | 8698 | 1991-12-03 | 950 | NULL | 30
|
| 8902 | FAKIR | ANALYST | 8566 | 1991-12-03 | 3000 | NULL | 20
|
| 8934 | MITA | CLERK | 8882 | 1992-01-23 | 1300 | NULL | 10
|
+-------+-----------+-----------+------+------------+------+------+--------
+
Question 3
Write a query to display employee name and salary of those employee who don't
have their salary in the range of 2500 to 4000.
Answer
SELECT ENAME, SAL
FROM empl
WHERE SAL NOT BETWEEN 2500 AND 4000;
Output
+----------+------+
| ENAME | SAL |
+----------+------+
| SMITH | 800 |
| ANYA | 1600 |
| SETH | 1250 |
| MOMIN | 1250 |
| AMIR | 5000 |
| KULDEEP | 1500 |
| SHIAVNSH | 2450 |
| ANOOP | 1100 |
| JATIN | 950 |
| MITA | 1300 |
+----------+------+
Question 4
Write a query to display the name, job title and salary of employee who do not have
manager.
Answer
SELECT ENAME, JOB, SAL
FROM empl
WHERE MGR IS NULL ;
Output
+-------+-----------+------+
| ENAME | JOB | SAL |
+-------+-----------+------+
| AMIR | PRESIDENT | 5000 |
+-------+-----------+------+
Question 5
Write a query to display the name of employee whose name contains 'A' as third
alphabet.
Answer
SELECT ENAME
FROM empl
WHERE ENAME LIKE '__A%' ;
Explanation
There are no employees whose name contains 'A' as the third alphabet in the empl
table. Therefore, the output will be empty.
Question 6
Write a query to display the name of employee whose name contains 'T' as the last
alphabet.
Answer
SELECT ENAME
FROM empl
WHERE ENAME LIKE '%T' ;
Output
+-------+
| ENAME |
+-------+
| SCOTT |
+-------+
Question 7
Write a query to display the name of employee whose name contains 'M' as first
alphabet 'L' as third alphabet.
Answer
SELECT ENAME
FROM empl
WHERE ENAME LIKE 'M_L%' ;
Explanation
There are no employees whose name contains 'M' as first alphabet and 'L' as third
alphabet in the empl table. Therefore, the output will be empty.
Question 8
Write a query on the customers table whose output will exclude all customers with a
rating <= 100, unless they are located in Shimla.
Answer
SELECT *
FROM customers
WHERE rating > 100 OR city = 'Shimla' ;
Question 9
Write a query that selects all orders (Order table) except those with zeros or NULLs
in the amt field.
Answer
SELECT *
FROM order
WHERE amt IS NOT NULL AND amt <> 0 ;
Question 10
Write SQL commands for the following on the basis of given table STUDENT :
Table : STUDENT
StudN Grade
Class Name GAME SUPW Grade2
o. 1
Basket
14 9 Archana A Literature A
Ball
1. Display the names of the students who are getting a grade 'C' in either GAME
or SUPW.
2. Display the different games offered in the school.
3. Display the SUPW taken up by the students, whose name starts with 'A'.
Answer
1.
SELECT Name
FROM STUDENT
WHERE Grade1 = 'C' OR Grade2 = 'C' ;
Output
+-------+
| Name |
+-------+
| Sujit |
| Veena |
| Arpit |
+-------+
2.
SELECT DISTINCT GAME
FROM STUDENT ;
Output
+-------------+
| GAME |
+-------------+
| Cricket |
| Tennis |
| Swimming |
| Basket Ball |
+-------------+
3.
SELECT SUPW
FROM STUDENT
WHERE Name LIKE 'A%' ;
Output
+------------+
| SUPW |
+------------+
| Literature |
| Gardening |
+------------+
Question 11
Write SQL commands for the following on the basis of given table SPORTS :
Table : SPORTS
1. Display the names of the students who have grade 'C' in either Game1 or
Game2 or both.
2. Display the names of the students who have same game for both Game1 and
Game2.
3. Display the games taken up by the students, whose name starts with 'A'.
Answer
1.
SELECT Name
FROM SPORTS
WHERE Grade1 = 'C' OR Grade2 = 'C' ;
Output
+-------+
| Name |
+-------+
| Sujit |
| Venna |
| Arpit |
+-------+
2.
SELECT Name
FROM SPORTS
WHERE Game1 = Game2 ;
Output
+-------+
| Name |
+-------+
| Venna |
+-------+
3.
SELECT Game1, Game2
FROM SPORTS
WHERE Name LIKE 'A%' ;
Output
+------------+-----------+
| Game1 | Game2 |
+------------+-----------+
| Basketball | Cricket |
| Cricket | Athletics |
+------------+-----------+
Question 12
Write SQL commands for the following on the basis of given table CLUB :
Table : CLUB
COA
COACHN AG SE DATOFAP
CH_I SPORTS PAY
AME E X P
D
Answer
1.
SELECT *
FROM CLUB
WHERE SPORTS = 'SWIMMING' ;
Output
+----------+-----------+-----+----------+-----+-----+------------+
| COACH_ID | COACHNAME | AGE | SPORTS | PAY | SEX | DATOFAPP |
+----------+-----------+-----+----------+-----+-----+------------+
| 5 | ZUBIN | 36 | SWIMMING | 750 | M | 1998-01-12 |
| 6 | KETAKI | 36 | SWIMMING | 800 | F | 1998-02-24 |
| 9 | KUSH | 41 | SWIMMING | 900 | M | 1998-01-13 |
+----------+-----------+-----+----------+-----+-----+------------+
2.
SELECT COACHNAME, DATOFAPP
FROM CLUB
ORDER BY DATOFAPP DESC ;
Output
+-----------+------------+
| COACHNAME | DATOFAPP |
+-----------+------------+
| KETAKI | 1998-02-24 |
| ZAREEN | 1998-02-22 |
| ANKITA | 1998-02-20 |
| KARAN | 1998-02-19 |
| SHAILYA | 1998-02-19 |
| RAVINA | 1998-01-20 |
| KUSH | 1998-01-13 |
| ZUBIN | 1998-01-12 |
| TARUN | 1998-01-01 |
| KUKREJA | 1996-03-27 |
+-----------+------------+
3.
SELECT COACHNAME, PAY, AGE, (PAY * 0.15) AS BONUS
FROM CLUB ;
Output
+-----------+------+-----+--------+
| COACHNAME | PAY | AGE | BONUS |
+-----------+------+-----+--------+
| KUKREJA | 1000 | 35 | 150.00 |
| RAVINA | 1200 | 34 | 180.00 |
| KARAN | 2000 | 34 | 300.00 |
| TARUN | 1500 | 33 | 225.00 |
| ZUBIN | 750 | 36 | 112.50 |
| KETAKI | 800 | 36 | 120.00 |
| ANKITA | 2200 | 39 | 330.00 |
| ZAREEN | 1100 | 37 | 165.00 |
| KUSH | 900 | 41 | 135.00 |
| SHAILYA | 1700 | 37 | 255.00 |
+-----------+------+-----+--------+
Question 13
Write SQL commands for the following on the basis of given table STUDENT1 :
Table : STUDENT1
Answer
1.
SELECT *
FROM STUDENT1
WHERE Stream = 'Nonmedical' ;
Output
+-----+--------+---------+------------+---------+-------+-------+
| No. | Name | Stipend | Stream | AvgMark | Grade | Class |
+-----+--------+---------+------------+---------+-------+-------+
| 5 | Sabina | 500 | Nonmedical | 90.6 | A | 11A |
| 8 | Rubina | 450 | Nonmedical | 88.5 | A | 12A |
| 9 | Vikas | 500 | Nonmedical | 92.0 | A | 12A |
+-----+--------+---------+------------+---------+-------+-------+
2.
SELECT Name
FROM STUDENT1
WHERE Class LIKE '12%'
ORDER BY Stipend ;
Output
+--------+
| Name |
+--------+
| Divya |
| Mohan |
| Arun |
| Karan |
| John |
| Rubina |
| Vikas |
+--------+
3.
SELECT *
FROM STUDENT1
ORDER BY AvgMark DESC ;
Output
+-----+---------+---------+------------+---------+-------+-------+
| No. | Name | Stipend | Stream | AvgMark | Grade | Class |
+-----+---------+---------+------------+---------+-------+-------+
| 9 | Vikas | 500 | Nonmedical | 92.0 | A | 12A |
| 5 | Sabina | 500 | Nonmedical | 90.6 | A | 11A |
| 2 | Divakar | 450 | Commerce | 89.2 | A | 11C |
| 8 | Rubina | 450 | Nonmedical | 88.5 | A | 12A |
| 1 | Karan | 400 | Medical | 78.5 | B | 12B |
| 6 | John | 400 | Medical | 75.4 | B | 12B |
| 4 | Arun | 350 | Humanities | 73.1 | B | 12C |
| 3 | Divya | 300 | Commerce | 68.6 | C | 12C |
| 10 | Mohan | 300 | Commerce | 67.5 | C | 12C |
| 7 | Robert | 250 | Humanities | 64.4 | C | 11A |
+-----+---------+---------+------------+---------+-------+-------+
4.
SELECT Name, Stipend, Stream, (Stipend * 12) AS Yearly_Stipend
FROM STUDENT1 ;
Output
+---------+---------+------------+----------------+
| Name | Stipend | Stream | Yearly_Stipend |
+---------+---------+------------+----------------+
| Karan | 400 | Medical | 4800 |
| Divakar | 450 | Commerce | 5400 |
| Divya | 300 | Commerce | 3600 |
| Arun | 350 | Humanities | 4200 |
| Sabina | 500 | Nonmedical | 6000 |
| John | 400 | Medical | 4800 |
| Robert | 250 | Humanities | 3000 |
| Rubina | 450 | Nonmedical | 5400 |
| Vikas | 500 | Nonmedical | 6000 |
| Mohan | 300 | Commerce | 3600 |
+---------+---------+------------+----------------+
Question 14
Consider the table Student1 of Q. 13. Give the output of following SQL statement :
Answer
1. It will return error because no argument is passed as decimal places to truncate.
Syntax of truncate function is TRUNCATE(number, decimals) .
2.
Output
+----------------+
| ROUND(AvgMark) |
+----------------+
| 78 |
| 73 |
| 75 |
+----------------+
3.
Output
+----------------------+
| CONCAT(Name, Stream) |
+----------------------+
| RubinaNonmedical |
| VikasNonmedical |
+----------------------+
4.
Output
+------------------+
| RIGHT(Stream, 2) |
+------------------+
| al |
| ce |
| ce |
| es |
| al |
| al |
| es |
| al |
| al |
| ce |
+------------------+
Question 15
Output
+--------------+
| MIN(AvgMark) |
+--------------+
| 64.4 |
+--------------+
2.
Output
+--------------+
| SUM(Stipend) |
+--------------+
| 1150 |
+--------------+
3.
Output
+--------------+
| AVG(Stipend) |
+--------------+
| 475 |
+--------------+
4. It will give an error because the COUNT function requires an argument specifying
what to count. Additionally, the DISTINCT keyword is followed by a column name to
count the distinct values of that column.
Question 16
Write SQL commands for the following on the basis of given table MOV :
Table : MOV
1. Display a list of all movies with Price over 20 and sorted by Price.
2. Display all the movies sorted by QTY in decreasing order.
3. Display a report listing a movie number, current value and replacement value
for each movie in the above table. Calculate the replacement value for all
movies as : QTY * Price * 1.15.
Answer
1.
SELECT Title
FROM MOV
WHERE Price > 20
ORDER BY Price ;
Output
+--------------------+
| Title |
+--------------------+
| Splash |
| Tootsie |
| Cocoon |
| Gone with the Wind |
| Risky Business |
| Top Gun |
| 101 Dalmatians |
| Friday the 13th |
| Crocodile Dundee |
+--------------------+
2.
SELECT Title
FROM MOV
ORDER BY Qty DESC ;
Output
+--------------------+
| Title |
+--------------------+
| Top Gun |
| Gone with the Wind |
| Splash |
| Independence Day |
| 101 Dalmatians |
| Friday the 13th |
| Risky Business |
| Cocoon |
| Crocodile Dundee |
| Tootsie |
+--------------------+
3.
SELECT No AS Movie_Number , Price AS Current_Value, (Qty * Price * 1.15) AS
Replacement_Value
FROM MOV ;
Output
+--------------+---------------+--------------------+
| Movie_Number | Current_Value | Replacement_Value |
+--------------+---------------+--------------------+
| 1 | 39.95 | 183.77000350952147 |
| 2 | 69.95 | 160.884992980957 |
| 3 | 49.95 | 402.09750614166256 |
| 4 | 29.95 | 103.3275026321411 |
| 5 | 19.95 | 68.8275026321411 |
| 6 | 44.95 | 103.38500175476074 |
| 7 | 31.95 | 73.48500175476073 |
| 8 | 69.95 | 160.884992980957 |
| 9 | 59.95 | 206.8275026321411 |
| 10 | 29.95 | 34.44250087738037 |
+--------------+---------------+--------------------+
Question 17
Write SQL commands for the following on the basis of given table Teacher :
Table : Teacher
Answer
1.
SELECT *
FROM Teacher
WHERE Department = 'History' ;
Output
+----+----------+-----+------------+--------+-----+------------+
| No | Name | Age | Department | Salary | Sex | Dateofjoin |
+----+----------+-----+------------+--------+-----+------------+
| 2 | Sharmila | 31 | History | 20000 | F | 1998-03-24 |
| 4 | Sangeeta | 35 | History | 40000 | F | 1999-07-01 |
| 6 | Shyam | 50 | History | 30000 | M | 1998-06-27 |
+----+----------+-----+------------+--------+-----+------------+
2.
SELECT Name
FROM Teacher
WHERE Sex = 'F' and Department = 'Hindi' ;
Explanation
There are no records in the Teacher table where the department is 'Hindi'. Hence,
there will be no output.
3.
SELECT Name, Dateofjoin
FROM Teacher
ORDER BY Dateofjoin ;
Output
+----------+------------+
| Name | Dateofjoin |
+----------+------------+
| Sandeep | 1996-12-12 |
| Jugal | 1997-01-10 |
| Shiv Om | 1997-02-25 |
| Shalakha | 1997-07-31 |
| Rakesh | 1997-09-05 |
| Sharmila | 1998-03-24 |
| Shyam | 1998-06-27 |
| Sangeeta | 1999-07-01 |
+----------+------------+