0% found this document useful (0 votes)
43 views1 page

CA3 Python Programming Test Overview

This document contains a summary of a class test conducted on December 20th, 2022 for the Programming Concepts with Python course. The test had 11 multiple choice and short answer questions worth a total of 25 marks. The questions covered topics like Python developers, pip, string formatting, loops, slicing, copying, integer and binary conversions, and substring counting. The document also lists which course outcomes each question addresses.

Uploaded by

Sukla Banerjee
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views1 page

CA3 Python Programming Test Overview

This document contains a summary of a class test conducted on December 20th, 2022 for the Programming Concepts with Python course. The test had 11 multiple choice and short answer questions worth a total of 25 marks. The questions covered topics like Python developers, pip, string formatting, loops, slicing, copying, integer and binary conversions, and substring counting. The document also lists which course outcomes each question addresses.

Uploaded by

Sukla Banerjee
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Department of Computer Application

RCC Institute of Information Technology


Canal South Road, Kolkata - 700015
____________________________________________________________________________________________

Test No.: CA-3 Date of Class Test: 20-12-2022


Course Name: Programming Concept with Python Course Code: MCAN 101
Semester: 1st Year, 1st Semester, MCA Academic Session: 2022-2023
Time: 1hr Full Marks: 25

Answer any five(5)- 1 x 5 =5

1. Who is the developer of Python programming?


a) Guido van Rossum b)Denis Ritchie c)Y.C. Khenderakar d)None

2. What does pip stand for python?


a) Pip Installs Python b) Pip Installs Packages c) Preferred Installer Program d) All of the above

3. What will be the output of the following Python expression if x=56.236, >> print("%.2f"%x)
a) 56.236 b) 56.23 c) 56.0000 d) 56.24

4. What will be the output of the following Python code snippet?


for i in [1, 2, 3, 4][::-1]:
print (i)
a) 4 3 2 1 b) error c) 1 2 3 4 d) none of the mentioned

5. What will be the output of the following Python code? print("abc. DEF".capitalize())
a) Abc. Def b) abc. Def c) Abc. Def d) ABC. DEF

6. What will be the output of the following Python code?


x = 'abcd'
for i in range(len(x)):
print(i)
a) error b) 1 2 3 4 c) a b c d d) 0 1 2 3
Answer any four(4)- 4 x5 =20

7. Compare and contrast the difference between List and strings with suitable examples. 5

8. What is shallow copy in python? Discuss with an example. How can it be avoided? 3+2

9. How to create an integer from a binary number in python. Also write how to get binary equivalent of an
integer? Discuss [Link]() in details. 1+1+3

10. Sow how a decimal number is created in python. Why is it used in python? What are their limitations
in comparison to floating point numbers? 2+1+2

11. Write a program in Python to print number of occurrence of a substring within a string in both ways
using slicing and string methods. 3+2

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

QN CO QN CO QN CO QN CO QN CO QN CO QN CO
1 CO1 2 CO1 3 CO3 4 CO3 5 CO3 6 CO3 7 CO3
8 CO3 9 CO3 10 CO3 11 CO4

QN: Question Number, CO: Course Outcome

Common questions

Powered by AI

Decimal numbers in Python can be created using the decimal module, which provides the Decimal class for handling decimal arithmetic, offering higher precision than floating-point arithmetic and reducing floating-point issues like representation and arithmetic errors. An example is decimal.Decimal('0.1') which avoids the imprecision observed with float(0.1). Despite these advantages, Decimal is generally slower than floats due to higher computational costs, and care must be taken to manage precision manually. Floats, meanwhile, are adequate for many practical applications given their speed but may suffer from precision issues in critical applications.

Python's reverse slicing, such as [::-1], is key for efficiently reversing sequences, reflecting its power in concise data structure manipulation. For example, using 'hello world'[::-1] efficiently reverses the string, crucial for real-world data processing tasks like palindrome detection, undo operations, and formatted output generation. Understanding slicing paves the way for exploiting Python lists and strings capabilities, leading to optimized and readable code areas where performance on sequences is critical.

Using printf-style formatting with "%f", a floating-point number can be formatted to a specified degree of precision. For example, given x = 56.236, the code `print("%.2f" % x)` outputs '56.24', rounding to two decimal places. In contrast, the format() method provides additional flexibility and readability, allowing for detailed formatting within a single call, e.g., `'{:.2f}'.format(x)`, producing the same result. The format() method is preferred due to its composability and readability in modern Python development over legacy printf-style formatting.

A shallow copy in Python creates a new object, but inserts references into it to the objects found in the original. Therefore, altering a mutable object within either the original or copied composite object often reflects immediately in the other. For instance, using copy.copy(list_original) on a list of lists results in a shallow copy. Any modification to a sublist affects both lists. To avoid issues tied to shallow copying, a deep copy, which duplicates everything recursively, can be employed. The copy.deepcopy() function from Python's copy module can be utilized for this purpose.

The range() function in Python generates a sequence of numbers, supporting iteration in for-loops without manually indexing sequences. It creates an iterable sequence of integers, starting from 0 if not otherwise specified. For instance, `for i in range(4): print(i)` outputs 0, 1, 2, 3, iterating from 0 up to, but not including, 4. This simplifies iteration over sequences without needing explicit indexing or list initialization, highlighting Python's internal iteration abstraction.

In Python, an integer can be created from a binary number using int() with a base argument, such as int('101', 2), which converts the binary string '101' into the integer 5. To get an integer's binary equivalent, one can use the bin() function, which returns a binary string prefixed with '0b'. For instance, bin(5) yields '0b101'. The string.format() method can be used to format output, including converting numbers into binary strings without prefixes, e.g., '{:b}'.format(5) yields '101'. This method provides additional flexibility in formatting output.

Lists and strings in Python are both sequence types, but they have distinct differences. Lists are mutable, meaning their contents can be changed, whereas strings are immutable, making them unchangeable once created. This implies operations that modify contents must create new strings. For example, a list can be modified using methods like append(), pop(), and remove(), whereas to modify a string, one might need to create a new one or use methods like replace() to achieve a similar outcome. Example: list = [1, 2, 3]; list[0] = 4 gives list as [4, 2, 3], whereas str = 'abc'; str[0] = 'z' is not valid and requires creating a new string str = 'z' + str[1:]

Slicing in Python allows one to manually iterate through a string to find a substring's occurrences by examining each sliced part. This method is labor-intensive and involves looping and comparing segments of the string to the target substring. In contrast, string methods like str.count() efficiently count the occurrences directly. For example, while one might slice 'hello hello' into parts and manually count occurrences of 'lo', the optimized `s = 'hello hello'; print(s.count('lo'))` achieves the desired result succinctly and efficiently. This showcases Python's powerful abstraction for string searching via methods over slicing.

Python's handling of data types, shown in list methods like append(), reflects its flexible, dynamic type system. Lists being mutable supports in-place operations like list.append(), adding elements directly without creating new objects, thus aligning with mutable sequence design. In contrast, strings' immutability reflects a different intent; methods like str.replace() always return new strings to maintain original data unaltered, crucial for data integrity in multi-threaded scenarios. This dichotomy emphasizes Python's thoughtful data structure implementation, balancing performance and mutability against stability and data security.

To reverse a string in Python, one can employ a loop structure alongside string concatenation. Although less idiomatic than using slicing (e.g., string[::-1]), a for loop together with incremental string concatenation illustrates fundamental iteration and string handling concepts. For example, reversing 'abcd' could be implemented with: `result = ''; for char in 'abcd': result = char + result; print(result)` outputs 'dcba', building the string backward. While this approach demonstrates iteration's low-level operations, it's less efficient and less readable compared to direct slicing or reversed()

You might also like