You are on page 1of 7

Python and SQL Test

Honor Code:

I pledge to follow the Honor Code and to obey all rules for taking this test as specified by the
proctor.
I understand that when asked to follow the Honor Code on this test, I must follow the rules
below.

·        There are 3 parts of questions to answer.  When following the Honor Code, the
candidate strictly observes the time allotted to complete this test which is 1.0 hour.

·        When following the Honor Code, the candidate must work entirely alone on this test. 
Access to computer, phone or any communication device is strictly prohibited.

·        When following the Honor Code, the candidate should not refer to any book or
material which might or might not help in answering the test.

·        When following the Honor Code, the candidate may not share information about any
aspect of the test with anybody who has or has not already taken the test.

·        When following the Honor Code, the candidate must direct all questions concerning
the test to the proctor.

·        When following the Honor Code, it is the candidate’s responsibility to obtain


clarification from the proctor if there are questions concerning the requirements of the
Honor Code. 

The duration of the complete test is 1.0 hour. (Python and SQL)

PYTHON

Instructions:

1. This (Python) test is of 25 marks in total, which are split as mentioned below.
2. Part 1 contains 5 basic questions regarding strings, lists, tuples, dictionaries and
functions. (14 marks)
3. Part 2 contains 2 questions of Lambda function and list comprehension. (3 marks)
4. Part 3 contains 2 questions relating to Pandas Dataframes. (8 marks)
5. Code to create the data frames for the tables required for Part 3 of the test.

import pandas as pd

import numpy as n
dict1 = {'ID':[1,2,3,4,5],'Name':['Alpha','Bravo','Charlie','Delta','Echo']}

dict2 = {'ID':[2,2,5,3,1,1,4,4], 

'Sport':['Cricket','Football','Basketball','Basketball','Table tennis','Cricket','Football','Basketball']}

dict3 = {'ID':[2,1,3,5,4], 'Marks':[30,' ',85,65,97]}

df1 = pd.DataFrame(data = dict1)

df2 = pd.DataFrame(data = dict2)

df3=  pd.DataFrame(data = dict3)

df3['Marks']=np.where(df3['ID']==1,np.nan,df3['Marks'])

7. Kindly save the .py file with the following naming convention:

Firstname_Lastname_Python_test.py 

(For example if your name is Mark Jones the document will be named
Mark_Jones_Python.py)
Part 1 (Basics, strings, lists, tuples, dictionaries)

1. Write a python function ‘convert’ to convert a python string to a python list of words: (4
marks)

example:

str = “ajit.singh@transorg.com”          list = [‘ajit’,’singh’,’transorg’,’com’]

2. Write a python program to create an intersection between two lists. (2 marks)

example:

l1 = [1,2,3,4,5,6] l2 = [2,3,4,7,8]

new_list = [2,3,4]

3. Write a python function “remove_duplicates” to remove duplicates from a list. (3 marks)

example:

l1 = [1,2,2,1,3,4]

new_list = [1,2,3,4]

4. Write a Python program to find the second largest number in a list. (3 marks)

l1= [100,101,102,103,104,105]

5. Write a python code to generate a list of tuples from a dictionary. (2 marks)

For example:      d = {1:2,2:3,3:4}           new_list = [(1,2), (2,3), (3,4)]

Part 2 (Lambda Functions)

Write a lambda function to square the elements in a list (1.5 marks)


l1= [1,2,3,4,5,6,7,8,9]

2. Write a lambda function to find the smaller value between two elements. (1.5 marks)
Example:
Input: 2,3
Output: 2
Part 3 (Pandas DataFrames):

Use the code in the instructions to create the dataframes for the 3 tables above.
Using the above 3 tables write python codes for the following questions:

1.Merge Table 1 and Table 2 on ID such that the resultant data frame has 3 columns in the
following order: ID, Name and Sport. The objective is to have all the sports played by a
person listed against their corresponding ID and Name. (4 marks)

Output:

ID Name Sports

1 Alpha Table Tennis

1 Alpha Cricket

2 Bravo Cricket

2 Bravo Football

3 Charlie Basketball

4 Delta Football

4 Delta Basketball

5 Echo Basketball

2.Find the number of ID’s having marks greater than 80. Also, find the maximum and
minimum values of the column “Marks” in table 3. (4 marks)
SQL

Instructions to follow

1. The test is of 20 marks and the marks for individual questions is written beside each
question.
2. Open your web browser and go to http://www.sqlfiddle.com/.
3. Select MySQL 5.6 as the compiler
4. Paste the schema for the tables (given on next page) in the schema panel and click
on build schema.
5. For each question, send the query as well as the result you obtained in a document
named Firstname_Lastname_SQL.docx (For example if your name is Mark Jones
the document will be named Mark_Jones_SQL.docx)

*If you already have a SQL compiler on your local system, you can use it as well.

Tables for Questions 1-5

employees

 
joining_date

PreWork: Copy the SQL command below to create schema of the above tables in the
Schema Panel of SQLFiddle.

CREATE TABLE employees


(`emp_id` varchar(5), `last_name` varchar(6), `first_name` varchar(6), `job_role`
varchar(14));

INSERT INTO employees


(`emp_id`, `last_name`, `first_name`, `job_role`)
VALUES
('E0011', 'Verma', 'Akhil', 'Administration'),
('E0012', 'Samson', 'Nikita', 'Asst. Manager'),
('E0013', 'Jordan', 'Nil', 'In charge'),
('E0014', 'Smith', 'Joe', 'Technician');
 
 
CREATE TABLE joining_date
(`emp_id` varchar(5), `joining_date` date);

INSERT INTO joining_date


(`emp_id`, `joining_date`)
VALUES
('E0012', '2016-04-18'),
('E0013', '2016-04-19'),
('E0014', '2016-05-01');

Write MySQL queries for the following questions


 

1. Find employee’s details (with joining date) for Emp_Id E0012 - 3 marks
 
2.     Find number of records with Technician Job_role - 3 marks

3.     Find Emp_Id who joined in 2016 - 4 marks

4.     Find employee’s Joining Date whose name start with “J” and end with “h” - 4 marks

5.     Find details of the most recently joined employee - 2 mark

 
Table for question 6.

                  t1 
sno room in_time

1 A 9:00

2 B 9:45

3 E 10:05

4 A 10:30

5 E 12:00

6 B 12:50
7 D 13:00

8 C 14:20

9 B 15:15

10 C 15:20

11 D 15:55

12 E 16:05

13 A 16:20

14 D 16:40

15 A 16:50

     
 

PreWork: Copy the SQL command below to schema of the above tables in the
Schema Panel of SQLFiddle.

CREATE TABLE t1
(`sno` int, `room` varchar(1), `in_time` time);

INSERT INTO t1
(`sno`, `room`, `in_time`)
VALUES
(1, 'A', '09:00'),
(2, 'B', '09:45'),
(3, 'E', '10:05'),
(4, 'A', '10:30'),
(5, 'E', '12:00'),
(6, 'B', '12:50'),
(7, 'D', '13:00'),
(8, 'C', '14:20'),
(9, 'B', '15:15'),
(10, 'C', '15:20'),
(11, 'D', '15:55'),
(12, 'E', '16:05'),
(13, 'A', '16:20'),
(14, 'D', '16:40'),
(15, 'A', '16:50');

6.   Person enters the building at 9:00 AM and exits at 17:00. Write the query to count the
number of times person enters each room - 4 marks

You might also like