0% found this document useful (0 votes)
7 views12 pages

ADUP

The document contains multiple-choice questions (MCQs) and explanations regarding Python programming concepts, including lists, tuples, lambda functions, and comprehensions. It also discusses unique features of Python, differences between Python 2 and 3, identifiers, keywords, indentation, file types in Linux, user input, type casting, and loops. The content is aimed at helping learners understand fundamental Python programming principles and practices.

Uploaded by

rutulj294
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)
7 views12 pages

ADUP

The document contains multiple-choice questions (MCQs) and explanations regarding Python programming concepts, including lists, tuples, lambda functions, and comprehensions. It also discusses unique features of Python, differences between Python 2 and 3, identifiers, keywords, indentation, file types in Linux, user input, type casting, and loops. The content is aimed at helping learners understand fundamental Python programming principles and practices.

Uploaded by

rutulj294
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
You are on page 1/ 12

1

ADUP
(APPLICATION DEVELOPMENT USING
PYTHON)

MCQs

Q1: Which of the following statements about lists in Python is true?


A) Lists are immutable
B) Lists are ordered collections
C) Lists do not allow duplicate values
D) Lists are defined using parentheses

Answer: B) Lists are ordered collections

Q2: What will be the output of the following code?

my_list = [1, 2, 3, "apple"]

print(my_list[2])

A) apple
B) 2
C) 3
D) Error

Answer: C) 3

Q3: Which function is used to create a sequence of numbers in Python?


A) list()
B) tuple()
C) range()
D) dict()

Answer: C) range()

Q4: What is a key difference between a tuple and a list?


A) Lists are immutable, but tuples are mutable
B) Tuples are immutable, but lists are mutable
C) Both lists and tuples are immutable
D) Tuples allow duplicate elements, while lists do not

Answer: B) Tuples are immutable, but lists are mutable


By: Akatsuki
2

Q5: What is a lambda function in Python?


A) A function that can take unlimited parameters
B) A function that is automatically executed at runtime
C) An anonymous function with a single expression
D) A function that can return multiple values

Answer: C) An anonymous function with a single expression

Q6: What is the output of the following code?

add = lambda x, y: x + y

print(add(3, 4))

A) 34
B) 7
C) lambda x, y: x + y
D) Error

Answer: B) 7

Q7: Which of the following represents correct list comprehension?


A) [x + 1 for x in range(5)]
B) {x + 1 for x in range(5)}
C) (x + 1 for x in range(5))
D) x + 1 for x in range(5)

Answer: A) [x + 1 for x in range(5)]

Q8: What will be the output of the following code?

squares = [x*x for x in range(1, 4)]

print(squares)

A) [1, 4, 9]
B) [0, 1, 4, 9]
C) [1, 4, 9, 16]
D) [1, 2, 3]

Answer: A) [1, 4, 9]

Q9: What is the output of the following code?

my_set = {1, 2, 3}

my_set.add(4)

print(my_set)

By: Akatsuki
3

A) {1, 2, 3, 4}
B) [1, 2, 3, 4]
C) (1, 2, 3, 4)
D) {4, 3, 2, 1}

Answer: A) {1, 2, 3, 4}

Q10: Which of the following methods removes an element from a set without
throwing an error if the element does not exist?
A) remove()
B) pop()
C) discard()
D) delete()

Answer: C) discard()

Q11: What is the output of the following code?

for i in range(2, 6):

print(i)

A) 1 2 3 4 5
B) 2 3 4 5
C) 2 3 4 5 6
D) 3 4 5 6

Answer: B) 2 3 4 5

Q12: Which loop is best suited when the number of iterations is unknown?
A) for loop
B) while loop
C) do-while loop
D) if-else loop

Answer: B) while loop

Q13: How do you access a value from a dictionary?


A) dict.get(key)
B) dict[key]
C) Both A and B
D) None of the above

Answer: C) Both A and B

Q14: What is the output of the following dictionary operation?

my_dict = {"name": "John", "age": 25}

By: Akatsuki
4

print(my_dict["name"])

A) name
B) John
C) Error
D) 25

Answer: B) John

Q15: What is the keyword used to define a function in Python?


A) function
B) define
C) def
D) fun

Answer: C) def

Q16: What will be the output of the following function call?

def multiply(x, y=5):

return x * y

print(multiply(2))

A) 2
B) 5
C) 10
D) Error

Answer: C) 10

Q17: Which of the following is not a valid arithmetic operator in Python?


A) +
B) //
C) **
D) <>

Answer: D) <>

Q18: What will be the output of the following code?

print(10 // 3)

A) 3.33
B) 3
C) 4
D) Error

By: Akatsuki
5

Answer: B) 3

Q19: Which statement is used to import a module in Python?


A) include
B) import
C) using
D) module

Answer: B) import

Q20: What is the output of the following code?

import math

print(math.sqrt(16))

A) 16
B) 4
C) 8
D) Error

Answer: B) 4

1. What are the unique features of Python that make it popular among developers?

Python is widely used due to its simplicity, versatility, and powerful libraries. The key
features that make Python popular are:

1. Easy to Read and Write:

• Python uses a simple syntax similar to the English language, making it easy to
learn and use.

• Example:

print("Hello, World!") # Simple syntax for printing

2. Interpreted Language:

• Python does not need compilation like C or Java. Instead, it runs line by line,
making debugging easier.

3. Dynamically Typed:

• Variables in Python do not require explicit type declaration.

• Example:

x = 10 # Integer

By: Akatsuki
6

x = "Hello" # Now x is a string

4. Object-Oriented and Procedural Programming Support:

• Python supports both procedural and object-oriented programming, allowing


flexible coding structures.

5. Extensive Libraries and Frameworks:

• Python has a wide range of libraries such as NumPy, Pandas, TensorFlow, and
Django, which help in AI, data science, web development, and more.

6. Platform-Independent:

• Python is cross-platform, meaning it can run on Windows, macOS, and Linux


without modification.

7. Open-Source & Large Community Support:

• Python is free to use, and it has a huge developer community that


continuously improves and supports the language.

2. What are the main differences between Python 2 and Python 3?

Feature Python 2 Python 3

print "Hello" (without


Print Statement print("Hello") (with parentheses)
parentheses)

5 / 2 → 2 (floor division by
Integer Division 5 / 2 → 2.5 (true division)
default)

Unicode Strings are Unicode by default (str


Strings are ASCII by default
Handling type)

Support No longer maintained Actively developed and improved

except Exception, e: (without


Error Handling except Exception as e: (with as)
as)

Uses range(), which behaves like


Iterators Uses xrange() for loops
xrange()

By: Akatsuki
7

Example Differences:

# Python 2

print "Hello" # Works in Python 2

print("Hello") # Error in Python 2

# Python 3

print("Hello") # Works in Python 3

Python 3 is the modern version and is recommended for all development work.

3. Explain Python Identifiers, Keywords, and Indentation.

A. Identifiers in Python

Identifiers are names used to identify variables, functions, classes, etc..

Rules for Identifiers:

1. Must start with a letter (A-Z, a-z) or underscore (_) but cannot start with a
number.

2. Cannot be a Python keyword (e.g., if, while, class).

3. Python is case-sensitive (MyVar and myvar are different).

Example:

myVariable = 10 # Valid identifier

_underscore = "Valid" # Valid identifier

2number = 20 # Invalid (Cannot start with a number)

class = 5 # Invalid (Cannot use keywords)

B. Keywords in Python

• Keywords are reserved words in Python that cannot be used as identifiers.

• Some Python keywords:

• False, None, True, and, as, assert, break, class, continue, def, del, elif, else,
except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass,
raise, return, try, while, with, yield

By: Akatsuki
8

C. Indentation in Python

• Unlike other languages that use {} or ;, Python uses indentation to define code
blocks.

• Indentation improves readability but also enforces structure.

Example of Correct Indentation:

if True:

print("Indented correctly") # Must be indented inside the block

Example of Incorrect Indentation (Error):

if True:

print("No Indentation") # ERROR - No indentation

4. Explain different types of Linux files. How can you get user input in Python and
work with different data types?

A. Types of Files in Linux

1. Regular Files (-): Text, scripts, or binary files.

2. Directory Files (d): Folders that store other files.

3. Special Files:

o Character Files (c): Represent devices like a keyboard.

o Block Files (b): Represent storage devices (hard drives).

4. Symbolic Links (l): Shortcuts to other files.

5. Sockets (s): Enable inter-process communication.

6. Named Pipes (p): Allow data transfer between processes.

B. Getting User Input in Python

Python uses input() to get user input.

name = input("Enter your name: ") # Takes input as a string

print("Hello, " + name)

C. Working with Different Data Types

By: Akatsuki
9

Python automatically assigns types based on input.

x = 10 # Integer

y = 10.5 # Float

z = "Hello" # String

is_valid = True # Boolean

We can check types using type():

print(type(x)) # Output: <class 'int'>

5. Explain the concept of Type Casting.

Type casting means converting a variable from one data type to another.

A. Implicit Type Casting (Automatic Conversion)

Python automatically converts a smaller type to a larger type.

x = 10 # int

y = 10.5 # float

z = x + y # Python converts int to float

print(z) # Output: 20.5

B. Explicit Type Casting (Manual Conversion)

We manually convert types using built-in functions:

# Convert integer to float

num = 10

float_num = float(num) # 10 → 10.0

# Convert string to integer

num_str = "100"

num_int = int(num_str) # "100" → 100

By: Akatsuki
10

# Convert integer to string

num_text = str(num_int) # 100 → "100"

Example of Type Casting:

age = input("Enter your age: ") # Always takes input as a string

age = int(age) # Convert to integer

print("Next year, your age will be:", age + 1)

6. Describe Lists, Ranges, and Tuples in Python

• Lists:
A list in Python is a collection that is ordered, mutable, and allows duplicate
elements. Lists are defined using square brackets []. Example:

my_list = [1, 2, 3, "apple", "banana"]

print(my_list[0]) # Output: 1

• Ranges:
A range in Python is used to generate a sequence of numbers. It is commonly
used in loops and is defined using the range() function. Example:

for i in range(1, 5):

print(i)

# Output: 1 2 3 4

• Tuples:
A tuple is a collection that is ordered, immutable, and allows duplicate
elements. It is defined using parentheses (). Example:

my_tuple = (1, 2, 3, "apple")

print(my_tuple[1]) # Output: 2

7. Explain Lambda Function with Example

By: Akatsuki
11

• A lambda function is an anonymous, single-line function that can have


multiple arguments but only one expression. It is defined using the lambda
keyword.

• Example:

• square = lambda x: x * x

• print(square(5)) # Output: 25

• Lambda functions are useful for short, throwaway functions that do not need to
be explicitly named.

8. Describe Comprehensions in Python with Example

• Comprehensions provide a concise way to create lists, dictionaries, and sets.

• List Comprehension:

squares = [x*x for x in range(1, 6)]

print(squares) # Output: [1, 4, 9, 16, 25]

• Dictionary Comprehension:

squares_dict = {x: x*x for x in range(1, 6)}

print(squares_dict)

# Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

• Set Comprehension:

unique_numbers = {x for x in [1, 2, 2, 3, 3, 4]}

print(unique_numbers) # Output: {1, 2, 3, 4}

9. Explain Different Types of Functions Used in Sets

• A set in Python is an unordered collection of unique elements.

• Common Set Functions:

1. add() – Adds an element to the set.

my_set = {1, 2, 3}

By: Akatsuki
12

my_set.add(4)

print(my_set) # Output: {1, 2, 3, 4}

2. remove() – Removes an element; raises an error if not found.

my_set.remove(2)

3. discard() – Removes an element; does not raise an error if not found.

4. union() – Combines two sets.

5. intersection() – Finds common elements between two sets.

10. Describe Loops in Python with Example

• For Loop:
Used to iterate over a sequence (list, tuple, range, etc.).

for i in range(1, 6):

print(i)

• While Loop:
Runs as long as a condition is True.

x=1

while x <= 5:

print(x)

x += 1

By: Akatsuki

You might also like