Computer Science
Predict the output of the following code:
d = {"apple": 15, "banana": 7, "cherry": 9}
str1 = ""
for key in d:
str1 = str1 + str(d[key]) + "@" + "\n"
str2 = str1[:-1]
print(str2)
Answer
15@
7@
9@
Working
Initialization:
- A dictionary
d
is created with three key-value pairs. - An empty string
str1
is initialized.
- A dictionary
Iteration:
Thefor
loop iterates over each key in the dictionary:- For "apple":
str1
becomes "15@\n" (value 15 followed by "@" and a newline). - For "banana":
str1
becomes "15@\n7@\n" (adding value 7 followed by "@" and a newline). - For "cherry":
str1
becomes "15@\n7@\n9@\n" (adding value 9 followed by "@" and a newline).
- For "apple":
- The line
str2 = str1[:-1]
removes the last newline character fromstr1
, resulting instr2
being "15@\n7@\n9@". - Finally,
print(str2)
outputs the content ofstr2
.
Related Questions
You have a stack named BooksStack that contains records of books. Each book record is represented as a list containing book_title, author_name, and publication_year.
Write the following user-defined functions in Python to perform the specified operations on the stack BooksStack:
push_book(BooksStack, new_book): This function takes the stack BooksStack and a new book record new_book as arguments and pushes the new book record onto the stack.
pop_book(BooksStack): This function pops the topmost book record from the stack and returns it. If the stack is already empty, the function should display "Underflow".
peep(BookStack): This function displays the topmost element of the stack without deleting it. If the stack is empty, the function should display 'None'.
Write the definition of a user-defined function
push_even(N)
which accepts a list of integers in a parameterN
and pushes all those integers which are even from the listN
into a Stack namedEvenNumbers
.Write function pop_even() to pop the topmost number from the stack and return it. If the stack is already empty, the function should display "Empty".
Write function Disp_even() to display all element of the stack without deleting them. If the stack is empty, the function should display 'None'.
For example: If the integers input into the list
VALUES
are: [10, 5, 8, 3, 12]Then the stack
EvenNumbers
should store: [10, 8, 12]Predict the output of the following code:
line = [4, 9, 12, 6, 20] for I in line: for j in range(1, I%5): print(j, '#', end="") print()
Consider the table ORDERS as given below
O_Id C_Name Product Quantity Price 1001 Jitendra Laptop 1 12000 1002 Mustafa Smartphone 2 10000 1003 Dhwani Headphone 1 1500 Note: The table contains many more records than shown here.
A) Write the following queries:
(I) To display the total Quantity for each Product, excluding Products with total Quantity less than 5.
(II) To display the orders table sorted by total price in descending order.
(III) To display the distinct customer names from the Orders table.
(IV) Display the sum of Price of all the orders for which the quantity is null.
OR
B) Write the output
(I)
Select c_name, sum(quantity) as total_quantity from orders group by c_name;
(II)
Select * from orders where product like '%phone%';
(III)
Select o_id, c_name, product, quantity, price from orders where price between 1500 and 12000;
(IV)
Select max(price) from orders;