0% found this document useful (0 votes)
18 views6 pages

Python Programming Exam Practice

Visit our website to download the KG App and learn all about Python Programming through our University Exam Preparation Course. You can also join our Telegram groups for placement assistance and GATE discussions. Subscribe to our full course using the referral code KGYT to get a 10% discount on GATE 2022 preparation.
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)
18 views6 pages

Python Programming Exam Practice

Visit our website to download the KG App and learn all about Python Programming through our University Exam Preparation Course. You can also join our Telegram groups for placement assistance and GATE discussions. Subscribe to our full course using the referral code KGYT to get a 10% discount on GATE 2022 preparation.
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

Visit our website

Download KG App

SANCHIT SIR YASH SIR


PROGRAMMING DAILY PRACTICE PAPER 15
[Link] the output :
s1={3, 4}
s2={1, 2}
s3=set()
i=0
j=0
for i in s1:
for j in s2:
[Link]((i,j))
i+=1
j+=1
print(s3)
a.{(3, 4), (1, 2)}
b. Error
c. {(4, 2), (3, 1), (4, 1), (5, 2)}
d. {(3, 1), (4, 2)}
[tcs 2019]
[Link] the output :
class A():
def disp(self):
print("A disp()")
class B(A):
pass

Learn all about Python Programming

University Exam Preparation Course


VINAY SIR SANCHIT SIR
obj = B()
[Link]()
a. Invalid syntax for inheritance
b. Error because when object is created, argument must be passed
c. Nothing is printed
d. A disp()
[Accenture 2019]
[Link] the output :
count = 1
def doThis():
global count
for i in (1, 2, 3):
count += 1
doThis()
print (count)
a. 4
b. 3
c. 2
d. 0
[Wipro 2019]
4. Find the output :
class student:
def __init__(self):
[Link] = 97
self.__cgpa = 8.7
def display(self):
print([Link])

Learn all about Python Programming

University Exam Preparation Course


VINAY SIR SANCHIT SIR
obj=student()
print(obj._student__cgpa)
[Link] program runs fine and 8.7 is printed
[Link] because private class members can’t be accessed
c. Error because the proper syntax for name mangling hasn’t been implemented
d. The program runs fine but nothing is printed
[wipro 2020]
5. Find the output :
print('ab cd-ef'.title())
a. Ab cd-ef
b. Ab Cd-ef
c. Ab Cd-Ef
d. None of the mentioned
[Accenture 2020]

Learn all about Python Programming

University Exam Preparation Course


VINAY SIR SANCHIT SIR
ANSWERS
1 (C) 2 (D) 3 (A) 4 (A) 5(C)
Ans. c
Explanation : Above code runs for

i=3j=1
i=4j=2
i=4j=1
i=5j=2
{(3, 1), (4, 1), (4, 2), (5, 2)}

Ans. d

Explanation : We created an object of class B that inherits class A and so disp()


method (of class A) becomes visible with reference to object obj. Option d is
correct

Ans. A

Explanation : Here the loop will execute 3 times i.e i=1,i=2,i=3. In each iteration,
count is incremented by 1.

Ans. a

Explanation : No issues with the code. It will work fine.

Ans. c

Explanation : title() : This method returns a string where the first character in
every word is upper case. Like a header, or a title.

Resume Preparation Training course

Interview Preparation course


YASH SIR SANCHIT SIR
To get 10% discount on GATE 2022 Full
course,
Use referral code

KGYT
on

SUBSCRIBE FOR FULL


COURSE

Telegram group links

Placement group GATE discussion group

VISIT OUR WEBSITE


YASH SIR SANCHIT SIR

Common questions

Powered by AI

Global variables in Python are accessible throughout the module from any function, unless shadowed by local declarations. Within functions, the 'global' keyword enables modification of these variables from outside the function scope, otherwise changes would remain local to the function. This keyword explicitly declares that all operations on the variable reference the globally defined instance, not a shadowing local version. Managing scope with global variables ensures consistent data handling across function calls and supports state persistence and change tracking across different execution contexts in a program .

The Python set operation ensures uniqueness by only storing distinct tuples; when duplicates are encountered, they're automatically disregarded due to sets' inherent behavior of maintaining only unique elements. This characteristic is significant because it simplifies handling data collections where uniqueness is required, such as removing duplicates from datasets, ensuring distinct elements in computations (e.g., set arithmetic operations), and providing efficient membership testing and merge operations. This reduces complications associated with data redundancy and enhances program reliability and performance .

Name mangling in Python renames private class members by prefixing them with '_ClassName' to create a unique identifier, thereby reducing naming conflicts and accidental access from outside classes. This mechanism benefits scenarios requiring enforced encapsulation, such as preventing unintended interaction with internal state variables in complex systems or during integration, ensuring controlled access and modification. Beneficial situations include preserving class integrity when extending or modifying class behavior, safeguarding subclass operations from unintended consequences .

In the context of nested loops, increment operators act within the inner loop to iterate over collections and produce combinations of outputs, such as the tuples added to a set in the given example. In Python, these operators maintain their scope within each loop, meaning variables 'i' and 'j' are incremented separately according to their specific loops' scope rules. The nested loops iterate independently over each other, generating unique combinations, thus enhancing the operational complexity and efficiency in execution by leveraging multiple variable states during iteration .

The inheritance mechanism in object-oriented programming allows class B to inherit methods from class A without redefining them. In the provided scenario, when an object of class B calls the 'disp' method, it executes 'disp' from class A due to the lack of an overridden 'disp' method in class B. This demonstrates how inheritance facilitates method reuse and confirms method visibility for derived class objects, thereby extending the parent class's functionalities to the child class and preserving method access within the class hierarchy .

In Python classes, public members are accessible from outside the class, intended for general interaction and modification. In contrast, private members, though technically accessible through mangling techniques, are intended to be shielded and used only within the class. This distinction suggests an encapsulation strategy where public members represent the class's interface, while private members maintain internal state integrity. The example illustrates these principles by declaring 'marks' as public and '__cgpa' as private, highlighting encapsulation benefits—maintaining a clean interface and reducing error-prone interactions with internal representations .

The 'title' method in Python capitalizes the first character of each word in a string. When applied to 'ab cd-ef', it results in 'Ab Cd-Ef'. This behavior occurs because the 'title' method considers delimiters like spaces and hyphens separately, treating them as breaking points between 'words.' Consequently, this leads to each segment, separated by these delimiters, beginning with an uppercase letter. Thus, the hyphen's presence does not alter the method's application, but systematically affects the capitalization of segments following the hyphen, treating each as distinct from others separated by spaces .

The code runs without error when accessing a supposedly private member '__cgpa' of the class 'student' due to Python's name mangling mechanism. In Python, private members are prefixed with double underscores to prevent accidental modification and are conventionally considered non-public. However, Python's name mangling translates '__cgpa' to '_student__cgpa', allowing access when explicitly using this syntax. Thus, despite being 'private,' accessing the attribute using the mangled name does not breach any access violation in Python, illustrating encapsulation's flexible enforcement in Python programming .

In the sets operation problem, the usage of nested loops to add tuple elements to a set illustrates the concept of iterating over iterable objects and modifying a data structure in Python. Specifically, it results in distinct tuple combinations being added to the set, showing a fundamental use of collections and loops . On the other hand, the global variable function showcases the scope of variables, specifically the 'global' keyword, which is necessary to modify a variable defined outside of local scope within a function. Both examples demonstrate essential Python concepts: iterations and the scope/resolution of variables .

The successful call to obj.disp() results from Python's inheritance mechanism, where class B, instantiated as obj, inherits all methods from class A, including 'disp()'. No method override occurs in class B, so 'disp()' from class A is executed. This illustrates the principle of method inheritance, where objects can utilize parent class methods, preserving functionality and reducing redundancy. It signifies that the instantiated object holds access to inherited methods, confirming inheritance's role in promoting code reuse and modular programming .

You might also like