Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Programming Midterm

The document outlines the curriculum for a course titled 'Proqramlaşdırmanın əsasları' taught by Misirli Roza at the Rəqəmsal texnologiyalar və tətbiqi informatika kafedrası. It includes a series of questions and answers covering fundamental programming concepts in Python, such as translation methods, comments, arithmetic operators, error types, loops, and list operations. The document serves as a study guide with practical examples and coding exercises for students.

Uploaded by

ali.alekber0700
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Programming Midterm

The document outlines the curriculum for a course titled 'Proqramlaşdırmanın əsasları' taught by Misirli Roza at the Rəqəmsal texnologiyalar və tətbiqi informatika kafedrası. It includes a series of questions and answers covering fundamental programming concepts in Python, such as translation methods, comments, arithmetic operators, error types, loops, and list operations. The document serves as a study guide with practical examples and coding exercises for students.

Uploaded by

ali.alekber0700
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Kafedranın adı Rəqəmsal texnologiyalar və tətbiqi informatika kafedrası

Müəllimin adı Misirli Roza


Fənnin kodu 01066
Tədris dili İNGİLİS
Fənnin adı Proqramlaşdırmanın əsasları
Sualların sayı 35

s/s Mövzu Sualın mətni


Explain translation methods in programming languages.

Answer : Translation methods convert high-level programming languages


into a form that a machine can understand and execute. Here are the primary
methods :

1 .Compilation : A compiler translates the entire source code of high-level


programming language into machine code (binary) in a single step, creating
an independent executable file. This process happens before the program
runs, so execution is faster. Examples of compiled languages : C, C++, Rust
1. 01
2. Interpretation : An interpreter translates and executes the source code
line-by-line, directly during runtime. No independent executable file is
created, making this method slower but flexible. Examples of
interpreted languages : Python, Ruby, JavaScript

3. Hybrid method : Combines elements of both compilation and


interpretation. The source code is first compiled into an intermediate
representation (bytecode), which is then executed by an interpreter
(virtual machine). Example : Java uses a compiler to generate bytecode,
which is executed by the Java Virtual Machine (JVM).
Explain single line and multiple line comments with examples in python

Answer : As you know commenting is very important in almost all


programming languages for compherensibility and flexibility. When
programmers write code they often execute codes step by step and sometimes
they need some explanation about any part of code. Comments divides into
two groups : single line and multiple line comments.

For better understanding, let me show it in python :


# We must enter birth year for calculating age
y = int(input(“Enter your birth year : “)
4. 01 a = 2025 – y
print(f”You have {a} years old.”)

Here you can see that we used # (hash) for single line commenting.

But if we want to show multiple line commenting it has 2 ways :

b = int(input("Balance: "))
l=1
"""Here we must denote l as 1 because it must enter while loop
And code must be executed"""
while l != 0 :
l = int(input("Please enter input : "))
if l == 0:
break
#When we don’t obtain any input then it means month is over
# We must stop loop
#For these reason we use break operation
b=b+l
w = int(input("Please enter withdrawal :"))
b=b-w
print(f"Your monthly balance is {b}")

As you see above, we used """ (triple quotes) or multiple # for multiple –
line commenting.

5. 01 Explain arithmetic operators in python with example

Explain syntax and semantic errors of Python programming languages

Answer : Syntax errors occur when the structure of the code violates the
rules of Python’s grammar. These errors prevent the code from being
executed or interpreted. Example : print(“Hello, world !)

This line has a mismatched quotation mark, resulting in a syntax error.


Python will raise an error like : unterminated string literal

Common causes :

• Missing or extra brackets, parantheses, or quotation marks.


• Incorrect indentation (Python enforces strict indentation rules).
6. 01 • Using reserved keywords incorrectly.

Semantic errors occur when the code runs successfully but does not perform
the intended function due to incorrect logic or meaning. Example : x = 10
y = 0 result = x / y It will give us ZeroDivisionError, indicating faculty
semantics

Common causes :

• Logical mistakes , such as incorrect formulae or algorithms.


• Misuse of variables or data types
• Confusion in how built-in functions or libraries work.

7. 01 Practical question

8. 02 Explain parsing and extracting with find method in string

9. 02 Explain lower(), upper(), swapcase() methods in python

10. 02 Explain strip(), rstrip(),lstrip(), startwith(), endwith() methods in pyhon


11. 02 Explain center(),capitalize(),count() methods in python

12. 02 Practical question

13. 03 Explain conditional statements in Python Programming languages

14. 03 Explain nested conditional statements in Python Programming languages

Write the code that displays the following output:

1234
0234
0134
0124
0123

15. 03 Answer :
s=0
for k in range(5):
for i in range(5):
if i == s:
continue
print(i, end = "")
s=s+1
print()
16. 03 Explain double split pattern content in string with example

17. 03 Practical question

18. 04 Explain f formating with examples

19. 04 Explain format() string method with examples

20. 04 Explain %s string formating with examples

21. 04 Add any string data in list and save it txt file

22. 04 Explain format() method, %s and f formating with examples

23. 05 Explain the range function (Range) and its parameters.

24. 05 Explain for loops in Python programming language with examples


25. 05 Give an example of applying the conditional operator in the for loop operator

Explain with examples by adding continue statement to for loop operator in Python
26. 05 programming language.

27. 05 Practical question

28. 06 Explain while (indefinite iteration) loops in Python programming language with examples

What is a random function? Describe the range of values of the random, randint, uniform,
and randrange functions

Answer : Python provides a module called random that contains several


functions to generate random numbers.

1. random() – It generates a random floating-point number between 0.0


and 1.0 (inclusive of 0.0 and 1.0)
29. 06 Example : import random
print(random.random())
Output : An integer like 0.23749

2. randint(a, b) – It generates a random integer between a and b


(inclusive of both)
Example : import random
print(random.randint(1,10))
Output : An integer like 7
3. uniform(a, b) – It generates a random floating-point number between
a and b (inclusive of both)
Example : import random
print(random.uniform(1.5, 5.5))
Output : A value like 3.72

4. randrange(start, stop, step) – It generates a random number from a


sequence defined by start , stop and step. It works like the range()
function but selects a random value wihin the specified range.
Example : import random
print(random.randrange(0, 10, 2))
Output : A value like 4
Make a maths quiz that asks five questions by randomly generating two whole numbers to
make the question (e.g. [num1] + [num2]). Ask the user to enter the answer. If they get it
right add a point to their score. At the end of the quiz, tell them how many they got correct
out of five

Answer : import random


def maths_quiz():
score = 0
num_questions = 5

print(“Welcome to the Maths Quiz!”)


print(f”Answer {num_questions} questions to test your
skills.\n”)
for i in range(1, num_questions + 1):
num1 = random.randint(1, 10)
30. 06 num2 = random.randint(1, 10)

print(f”Question {i}: What is {num1} + {num2}?”)


user_answer = int(input(“Your answer: “)

correct_answer = num1 + num2


if user_answer == correct_answer:
print(“Correct!\n”)
score += 1
else:
print(f”Wrong. The correct answer is
{correct_answer}.\n”)
print(f”You’ve completed the quiz! Your score: {score}”)
print(“Thank you for playing”)

31. 06 Write capitalize function with while loop and save output in the data. txt file

32. 06 Practical question

33. 07 Explain the concept of lists in Python programming language.

34. 07 Explain the list methods used in Python programming language with examples.
35. 07 Explain nested list and give examples

You are given list = [1, 4, 7, 5, 10]


36. 07 Find smallest element greater than p. (p=6)

37. 07 Practical question

You might also like