
Question Paper Code : 170000TB B.E./B.Tech. DEGREE EXAMINATION, DECEMBER 2017 First Semester GE17151 – PROBLEM SOLVING AND PYTHON PROGRAMMING (Common to ALL Branches) (Regulations 2017) Answer Key PART A 1. Define an algorithm. Algorithm is an ordered sequence of finite, well defined, unambiguous instructions for completing a task. 2. Describe recursion. A function that calls itself is recursive; the process of executing it is called recursion. For example, we can write a function that prints a string n times: def print_n(s, n): if n <= 0: return print(s) print_n(s, n-1) 3. List out the uses of default arguments in python. A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument. The following example gives an idea on default arguments, it prints default age if it is not passed: def printinfo(name, age = 19): print ("Name :", name) print ("Age :", age) printinfo("Arun", 20) printinfo("Babu") 4. Give the various data types in Python. Int, Float, String, Boolean, Complex, List, Tuple, Set, Dictionary. 5. Differentiate global variable from local variable. Variables that are defined inside a function body have a local scope, and those defined outside have a global scope. 6. What is slicing? A segment of a string is called a slice. Selecting a slice is similar to selecting a character. Example: >>> s = 'Monty Python' >>> s[0:5] 'Monty' >>> s[6:12] 'Python' 7. Compare list and tuple. A tuple is a sequence of values. The values can be any type, and they are indexed by integers, so in that respect tuples are a lot like lists. The important difference is that tuples are immutable. 8. What will be the output? >>>m = [[x, x + 1, x + 2] for x in range (0, 3)] No output. m = [[0, 1, 2], [1, 2, 3], [2, 3, 4]] 9. What is the output when following code is executed? >>>list1 = [1, 3] >>>list2 = list1 >>>list1[0] = 4 >>>print(list2) [4, 3] 10. Define read() and write() operations in a file. The read(size) method is used to read in size number of data. If size parameter is not specified, it reads and returns up to the end of the file. >>> f = open("rec.txt",'r') >>> f.read(3) # read the first 3 data 'Wel' The write method puts data into the file: >>> line1 = "This here's the wattle,\n" >>> fout.write(line1) 24 2 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai PART B 11. (a) (i) Build an algorithm to check the given number is prime number or not. (7) Step 1: Start Step 2: Declare variables n, i, flag. Step 3: Initialize variables flag←1 i←2 Step 4: Read n from user. Step 5: Repeat the steps until i<(n/2) 5.1 If remainder of n÷i equals 0 flag←0 Go to step 6 5.2 i←i+1 Step 6: If flag=0 Display n is not prime else Display n is prime Step 7: Stop 11. (a) (ii) Draw a flow chart to find the factorial of a given number. (6) 11. (b) (i) Explain in detail about the basic organization of a computer. (8) Components of computer hardware: 2 marks The computer system hardware comprises of three main components: 1. Input/Output (I/O) Unit, 2. Central Processing Unit (CPU), and 3. Memory Unit. B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 3 2 marks Input/Output Unit - The user interacts with the computer via the I/O unit. The Input unit accepts data from the user and the Output unit provides the processed data i.e. the information to the user. The Input unit converts the data that it accepts from the user, into a form that is understandable by the computer. Similarly, the Output unit provides the output in a form that is understandable by the user. The input is provided to the computer using input devices like keyboard, trackball and mouse. Some of the commonly used output devices are monitor and printer. 2 marks Central Processing Unit - CPU controls, coordinates and supervises the operations of the computer. It is responsible for processing of the input data. CPU consists of Arithmetic Logic Unit (ALU) and Control Unit (CU). • ALU performs all the arithmetic and logic operations on the input data. • CU controls the overall operations of the computer i.e. it checks the sequence of execution of instructions, and, controls and coordinates the overall functioning of the units of computer. Additionally, CPU also has a set of registers for temporary storage of data, instructions, addresses and intermediate results of calculation. 2 marks Memory Unit - Memory unit stores the data, instructions, intermediate results and output, temporarily, during the processing of data. This memory is also called the main memory or primary memory of the computer. The input data that is to be processed is brought into the main memory before processing. The instructions required for processing of data and any intermediate results are also stored in the main memory. The output is stored in memory before being transferred to the output device. CPU can work with the information stored in the main memory. Another kind of storage unit is also referred to as the secondary memory of the computer. The data, the programs and the output are stored permanently in the storage unit of the computer. Magnetic disks, optical disks and magnetic tapes are examples of secondary memory. - 2 marks 11. (b) (ii) Summarize the difference between algorithm, flow chart and pseudo code. (5) 1 mark Algorithm is an ordered sequence of finite, well defined, unambiguous instructions for completing a task. 1 mark A flowchart is a diagrammatic representation of the logic for solving a task. The purpose of drawing a flowchart is to make the logic of the program clearer in a visual form. 4 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 1 mark Pseudo code consists of short, readable and formally-styled English language used for explaining an algorithm. A pseudo code is easily translated into a programming language. Generally, programmers prefer to write pseudo code instead of flowcharts. 2 marks An algorithm can be represented using a pseudo code. Pseudo code is a readable, formally styled English like language representation of the algorithm. Pseudo code use structured constructs of the programming language for representation. The user does not require the knowledge of a programming language to write or understand a pseudo code. 12. (a) (i) Write a program to find whether the given year is leap year or not. (8) 7 marks Program: year = int(input("Enter the year : ")) if year % 400 == 0: print(year, "is a leap year") elif year % 4 == 0 and year % 100 != 0: print(year, "is a leap year") else: print(year, "is not a leap year") 1 mark Output: Enter the year : 2012 2012 is a leap year Enter the year : 2013 2013 is not a leap year 12. (a) (ii) Write a program to print the digit at first and hundredth place of a number. (5) 4 marks Program: n = int(input("Enter the number : ")) print("The digit at first place :", n % 10) print("The digits at hundred place :", n % 100) 1 mark Output: Enter the number : 786 The digit at first place : 6 The digits at hundred place : 86 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 5 12. (b) What are the different looping statements available in python? Explain with suitable examples. (13) 3 marks Iteration: Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that computers do well and people do poorly. In a computer program, repetition is also called iteration. while statement: 1 mark Syntax: while(test-condition): body of the loop statement-x 1 mark Flowchart: 3 marks Example: n = 4 while n > 0: print(n) n = n - 1 print('Blastoff!') The flow of execution for a while statement: 1. Determine whether the condition is true or false. 2. If false, exit the while statement and continue execution at the next statement. 3. If the condition is true, run the body and then go back to step 1. This type of flow is called a loop because the third step loops back around to the top. The body of the loop should change the value of one or more variables so that the condition becomes false eventually and the loop terminates. Otherwise the loop will repeat forever, which is called an infinite loop. 6 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai for Statement: A for statement is also called a loop because the flow of execution runs through the body and then loops back to the top. The syntax of a for statement has a header that ends with a colon and an indented body. The body can contain any number of statements. 1 mark Syntax: for variable in sequence: body of the loop statement-x 1 mark Flowchart: 3 marks Example: for i in range(4): print('Hello!') You should see something like this: Hello! Hello! Hello! Hello! This is the simplest use of the for statement. In this case, it runs the body four times. B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College | Chennai 7 13. (a) (i) Explain the different types of functions with examples. (8) 1 marks Types of Functions: There are two types of functions: •Built-in functions •User-defined functions 1 mark Function Definition: A function definition specifies the name of a new function and the sequence of statements that run when the function is called.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages36 Page
-
File Size-