Viva Questions
Viva Questions
o Pickling is the process of converting a Python object into a byte stream for storage
or transfer. Unpickling is the reverse process, converting the byte stream back into
a Python object.
o Lists are mutable (modifiable), while tuples are immutable (read-only). Lists use
square brackets [], and tuples use parentheses ().
o Python provides types like integers, floats, complex numbers, strings, lists, tuples,
dictionaries, sets, and boolean.
o The pass statement is a placeholder that does nothing. It's used where a statement
is syntactically required but no code is needed.
o Slicing extracts parts of sequences like lists, strings, or tuples using the syntax
start:stop:step.
o Negative indices access elements from the end of a sequence. For example, -1
refers to the last element.
12. What are the rules for local and global variables in Python?
o Local variables are defined inside a function and have limited scope. Global
variables are defined outside any function and can be accessed throughout the
program.
o The // operator performs floor division, returning the largest integer less than or
equal to the division result.
o Literals are fixed values in a program, such as numeric, string, boolean, and special
literals like None.
o Functions are blocks of reusable code that perform a specific task. They are defined
using the def keyword.
o Used for error handling. Code in try is executed, and errors are caught in except.
o An open-source RDBMS.
o Use INSERT INTO for records and ALTER TABLE for columns.
49. What is the difference between del and remove methods of list?
o Use str.lower().
54. print(‘Ayushi’.isupper())
o Output: False.
57. if i == 3:
58. continue
59. print(i)
o Output: 0, 1, 2, 4, 5, 6.
62. How do you calculate the number of characters of the string ‘Ayushi Sharma’?
63. How will you print the string ‘Python’ five times?
o Use 'Python' * 5.
o Lists are ordered and indexed by position; dictionaries are unordered and indexed
by keys.
...
Python libraries are reusable code collections. Examples: NumPy (numerical computations),
Pandas (data manipulation).
import module_name
65. What is the difference between primary key and candidate key?
A primary key is the chosen unique identifier for a table. Candidate keys are potential
primary keys.
NOW() returns the current date and time; CURRENT_DATE() returns the date only.
Use SQL JOINs like INNER JOIN, LEFT JOIN, or RIGHT JOIN:
The user_login function prompts the user to input a username and password. It checks the entered
credentials against predefined values (username="admin" and password="bps@2024"). If the
credentials match, it returns True, indicating a successful login. Otherwise, it returns False and
prints an "Invalid credentials" message.
The main_menu function presents the main menu options to the user, allowing them to select
different actions such as showing databases, creating a database, adding students, updating
records, sorting records, etc. The function runs in a loop, offering the user a chance to select
options until the user chooses to quit the program (option 16).
The choose_database function allows the user to select a database from the available databases on
the MySQL server. It first lists all the databases, then prompts the user to choose one by entering
the corresponding number. It sets the selected database as the global variable DATABASE.
The add_student function allows the user to add a student to the database. The user is prompted to
input the student's roll number, name, stream, total marks, grade, class, and batch. The function
inserts this data into the selected table and adds the student to a specific batch (using the
add_student_to_batch function).
The display_students_by_batch function displays student details (like roll number, name, stream,
total marks, grade, and class) for each batch stored in the BATCHES dictionary. It retrieves the
student information from the database for each roll number in the batch.
The add_student_to_batch function adds the student (identified by their roll number) to a specific
batch. The batches are stored in the BATCHES dictionary, where the key is the batch name, and the
value is a list of student roll numbers. If the batch does not exist, it creates a new entry.
The get_grade_conditions function gathers grade conditions from the user. It allows the user to
input grade thresholds (minimum marks required for each grade), and these conditions are
returned as a list of tuples containing the grade and the minimum marks. The user can input
multiple grades or enter "done" to finish.
The calculate_grade function calculates the grade of a student based on their marks and the grade
conditions. It iterates through the list of grade conditions (as returned by get_grade_conditions)
and assigns the highest grade that meets the condition of the student's marks.
10. How does the sort_records function work?
The sort_records function allows the user to sort records in the selected table based on a column
and the specified order (ascending or descending). The user is prompted for the column name and
the order (ASC for ascending or DESC for descending). The function then constructs a SELECT
query to retrieve the sorted records and displays them.
The show_databases function lists all the databases available in the MySQL server. It executes the
SQL query SHOW DATABASES and prints the names of the databases.
The choose_table function allows the user to choose a table from the available tables in the
selected database. It first lists all the tables and prompts the user to input the name of the table
they want to work with.
The create_table function creates a new table in the selected database if both DATABASE and
TABLE are set. The table has columns for roll number, name, stream, total marks, grade, and class.
If the table creation is successful, a confirmation message is printed.
The show_tables function lists all the tables present in the selected database. It executes the SQL
query SHOW TABLES to retrieve the table names.
The display_structure function shows the structure (schema) of the current table. It executes the
DESC {TABLE} SQL query to display the column names, data types, and other properties of the
table.
Let me know if you need additional details or further clarification on any of the questions!
4o mini
You said:
ChatGPT said:
ChatGPT
It seems like you are asking for answers to a comprehensive set of questions that might arise from
the code you shared, or perhaps you are referring to questions related to the code's logic and
functionality. I’ll assume you meant a breakdown of what the code does in a general sense, and I’ll
go through the questions systematically.
Here are answers to the questions that might arise from this code:
The connect_to_database function is used to establish a connection to the MySQL database. It does
this by calling mysql.connector.connect() with parameters like host, user, password, and database
name. It returns a connection object that can be used to interact with the MySQL database. The
function is designed to return the connection for use in other parts of the program.
The user_login function is a simple login function that prompts the user for a username and
password. It checks whether the entered credentials match predefined ones (username="admin",
password="bps@2024"). If the credentials match, it returns True, indicating a successful login;
otherwise, it returns False, showing "Invalid credentials."
3. What does the main_menu function do?
The main_menu function presents a list of options to the user, allowing them to interact with the
database. It provides actions such as viewing databases, creating a database, adding students,
sorting records, updating student information, and quitting the application. The user chooses
options by entering corresponding numbers, and the menu loops until the user selects the option to
quit.
The choose_database method allows the user to select a database from the list of databases
present on the MySQL server. It queries the list of databases and then prompts the user to choose
one. Once the database is chosen, it sets the DATABASE variable to the selected database, which is
used for subsequent operations like creating tables or adding records.
The add_student function enables the user to add a new student to the database. It asks for details
like roll number, name, stream, total marks, grade, class, and batch. The student’s information is
then inserted into the selected database using an INSERT INTO SQL statement. The function also
calls add_student_to_batch to associate the student with a specific batch.
The display_students_by_batch function displays student records by batch. It fetches the list of
students from each batch (stored in the BATCHES dictionary) and retrieves the details of each
student from the database. The student details include roll number, name, stream, total marks,
grade, and class.
The add_student_to_batch function adds a student (identified by their roll number) to a batch. It
uses a dictionary called BATCHES, where the key is the batch name, and the value is a list of
student roll numbers. If the batch does not already exist, the function creates a new entry in the
dictionary for that batch. The student is then added to the corresponding list of students.
The get_grade_conditions function collects grade conditions from the user. It asks the user to input
grade thresholds (minimum marks for each grade). For example, it may ask for the number of
marks required for each grade like A, B, C, etc. The user can input these thresholds for multiple
grades and finish the input by typing "done". The function returns these conditions as a list of
tuples.
The calculate_grade function determines the grade of a student based on their marks and the
grade conditions (which are retrieved by the get_grade_conditions function). It compares the
student's marks to the grade conditions, and the highest grade that meets the condition is
assigned to the student.
The sort_records function allows the user to sort records in a database table. The user is prompted
to select a column by which to sort, as well as the sorting order (ASC for ascending or DESC for
descending). The function constructs an SQL SELECT query with the ORDER BY clause and executes
it to display the sorted results.
The show_databases function retrieves and lists all the databases present on the MySQL server. It
executes the SQL query SHOW DATABASES and prints each database name, allowing the user to
view all available databases.
12. How does the choose_table function work?
The choose_table function lets the user select a table from the available tables in the chosen
database. The user is first shown a list of tables in the database and then prompted to enter the
name of the table they want to work with. The function stores this choice for later operations, such
as querying or modifying records in that table.
When the create_table function is called, it creates a table in the current database. The table
consists of columns for the student’s roll number, name, stream, total marks, grade, and class. If
the table is successfully created, a success message is printed. The function relies on the selected
database being active and available.
The show_tables function lists all the tables in the selected database. It executes the SHOW TABLES
SQL query, which retrieves the names of all tables in the current database, and prints them for the
user.
The display_structure function shows the structure (schema) of the current table. It executes the
DESC {TABLE} SQL command, which retrieves information about the columns in the table, such as
the column names, data types, and other attributes. This allows the user to view the layout of the
table.
NOTES
Functions: input() to get user choice, print() to display the menu and results.
Purpose: This experiment involves converting a while loop to a for loop, likely using
range() to iterate a specific number of times.
Functions: input() for user input, math.sqrt() for square root calculation.
Commands: Conditional checks (if-else) to check for real and complex roots.
Functions: input() for user input, min() and max() to find the youngest and oldest.
Functions: input() for user input, for loop for checking divisibility.
Modules: re (regular expressions) for pattern matching to validate the phone number
format.
10. Count Lines Starting with Digit in File (Experiment No. 10)
Commands: pickle.dump() and pickle.load() for writing and reading from binary files.
Commands: List methods like append() for pushing and pop() for popping elements.
Commands: SQL queries like SELECT, JOIN, GROUP BY, ORDER BY, WHERE, and COUNT.
Python Programs
Concepts:
Docstrings: Lines starting with triple quotes (""") are used to document the code's purpose
and functionality.
Comments: Lines starting with # are comments and are ignored by the Python interpreter.
Input/Output: The input() function is used to get user input, and the print() function is used
to display output.
Loops: for and while loops are used to iterate over sequences of data.
Conditional Statements: if, elif, and else statements are used to control the flow of the
program based on conditions.
Functions: Python functions are defined using the def keyword and can be used to
encapsulate reusable blocks of code.
Explanation:
The document outlines various Python programs but doesn't provide the complete code for each.
However, it mentions the concepts used in these programs.
2. SQL Queries
Concepts:
SQL (Structured Query Language): A language used to interact with relational databases.
INSERT INTO: Statement used to insert new rows of data into a table.
Explanation:
The document presents various SQL queries but doesn't provide the complete code for each query.
However, it mentions the concepts used in these queries.
While the document doesn't provide complete code examples, here are explanations for some
concepts mentioned:
This concept likely involves a function that takes a list as input and swaps the elements in the first
half with the elements in the second half. The function might achieve this by:
2. Slicing the list into two halves (first half and second half).
5. Assigning the temporary variable (originally containing the second half) to the second half
of the list (now containing the first half elements).
Example 2: Counting lines starting with a digit in a text file (Explanation without code)
This concept likely involves a function that takes the filename as input and returns the number of
lines starting with a digit. The function might achieve this by: