12 CS Set B Anskey
12 CS Set B Anskey
2. **Cardinality:** The cardinality of a table refers to the number of rows (or records) in the
table.
- The ORDERS table contains the following records:
- 101
- 102
- 103
- 104
- Therefore, the **cardinality** of the ORDERS table is **4**.
### b. Identify the Column which can serve as a primary key
A primary key is a column (or a combination of columns) in a database table that uniquely
identifies each row in that table. The primary key must contain unique values and cannot
contain NULL values.
Q.20 Let's analyze the provided Python code step by step. 2
```python
def modifyList(x):
x.append(sum(x))
print(x, end=' ')
L = [1, 2, 3, 4]
print(L)
modifyList(L)
The code defines a function `modifyList(x)` that takes a
list `x` as an argument. Inside the function:
- `x.append(sum(x))` calculates the sum of the current
elements in the list `x` and appends this sum to the list.
- `print(x, end=' ')` prints the modified list `x` followed
by a space (instead of a newline).
2. The list `L` is initialized with the values `[1, 2, 3, 4]`.
3. The first `print(L)` statement outputs the initial list:
```
[1, 2, 3, 4]
4. The `modifyList(L)` function is then called, passing the
list `L`:
- Inside `modifyList`, `sum(x)` is calculated. The sum of
`1 + 2 + 3 + 4` equals `10`.
- `10` is appended to the list `L`, which now changes `L`
to `[1, 2, 3, 4, 10]`.
- The modified list is printed, resulting in:
```
[1, 2, 3, 4, 10]
Q.21 The provided Python code contains several syntax errors and a misuse of function 2
parameters. Let's identify and correct the errors step by step.
```python
def say(message, times=1): # Corrected DEF to def and added colon
print(message * times, end=' ') # Corrected the print statement
```python
def Changer(P, Q=10):
P=P/Q
Q=P%Q
return P
A = 200
B = 20
A = Changer(A, B)
print(A, B)
B = Changer(B)
print(A, B)
```
1. **Function Definition:** The function `Changer` takes two parameters, `P` and `Q`, with
`Q` having a default value of 10. Inside the function:
- `P` is divided by `Q`, and the result is assigned back to `P`.
- `Q` is reassigned to the remainder of `P` divided by `Q`.
- The function returns the modified value of `P`.
2. **Initialization:**
- `A` is initialized to `200`.
- `B` is initialized to `20`.
3. **First Function Call:**
- `A = Changer(A, B)` translates to `A = Changer(200, 20)`.
- Inside `Changer(200, 20)`:
- `P` becomes `200 / 20 = 10.0`.
- `Q` is then updated to `10.0 % 20`, which is `10.0` (since 10.0 is less than 20).
- `Changer` returns `10.0`, so now `A` is `10.0`.
- The first `print(A, B)` outputs:
```
10.0 20
```
The `POW` (or `POWER`) function in SQL raises the first argument to the power of the second
argument. In this case, you are raising \(2\) to the power of \(3\):
\[
2^3 = 8
\]
**Output of a:**
8
### b. `select round(342.92,1)`
The `ROUND` function in SQL rounds a numeric value to a specified number of decimal places. In this
instance, you are rounding \(342.92\) to \(1\) decimal place:
- \(342.92\) rounded to one decimal place becomes \(342.9\) (since the digit after the first decimal
place is \(2\), which does not round up).
**Output of b:**
Q.24 Certainly! Below is a Python function named `countbb()` that reads from a text file named 2
`article.txt`, counts the occurrences of the words "bat" and "ball" (in a case-insensitive manner), and
then displays the counts.
```python
def countbb():
# Initialize counters for 'bat' and 'ball'
count_bat = 0
count_ball = 0
try:
# Open the file in read mode
with open('article.txt', 'r') as file:
# Read the content of the file
content = file.read()
except FileNotFoundError:
print("The file 'article.txt' was not found.")
except Exception as e:
print(f"An error occurred: {e}")
```sql
SELECT CoachID, CoachName, Pay
FROM Club
ORDER BY Pay DESC;
```
```sql
UPDATE Club
SET Pay = 1100
WHERE CoachName = 'KUKREJA';
```
```sql
DELETE FROM Club
WHERE Sports = 'SWIMMING';
```
### d. Display the coaches who coach Karate, ordered by age in ascending order.
```sql
SELECT CoachID, CoachName, Age
FROM Club
WHERE Sports = 'KARATE'
ORDER BY Age ASC;
"KUKREJA" in the `Club` table.
SECTION C
Q.26 You can achieve this by defining the function `SumDiv(L, x)` that iterates through the list `L`, checks if 3
each element is divisible by either `x` or `x + 1`, and sums the qualifying elements. Here’s how you
can implement this function in Python:
```python
def SumDiv(L, x):
# Initialize the sum
total_sum = 0
return total_sum
# Example usage
L = [10, 27, 12, 20, 22]
x=5
result = SumDiv(L, x)
print(result) # Output: 42
### Explanation:
1. **Initialization**: The variable `total_sum` is initialized to zero to store the cumulative sum of the
qualifying numbers.
2. **Iteration**: The function iterates over each integer in the list `L`.
3. **Divisibility Check**: Inside the loop, it checks if the current number `num` is divisible by `x`
(`num % x == 0`) or `x + 1` (`num % (x + 1) == 0`).
4. **Summation**: If either condition is true, the number is added to `total_sum`.
5. **Return Value**: Finally, the function returns the computed `total_sum`.
Q.27 Let's go through each of the queries and tasks you've mentioned: 3
### a. Identify the attribute best suitable to be declared as a primary key for the 'Employee'
table.
The attribute that is best suitable to be declared as a primary key for the 'Employee' table is
**Employee_ID**.
**Rationale**:
- A primary key must uniquely identify each record in the table. The `Employee_ID` attribute
is likely designed to be a unique identifier for each employee and would meet the
requirements for a primary key by ensuring that no two employees have the same
`Employee_ID`.
### b. Write an appropriate SQL query to modify the column size of 'First_Name' from 15
characters to 30 characters.
To modify the column size of `First_Name` from 15 characters to 30 characters, you can use
the following SQL `ALTER TABLE` statement:
```sql
ALTER TABLE Employee
MODIFY First_Name CHAR(30);
```
**Note**: The exact syntax may vary slightly depending on the database system you are
using (e.g., MySQL, Oracle, SQL Server, etc. In some systems, you may use `VARCHAR`
instead of `CHAR`, and the keyword `MODIFY` may be different, such as `ALTER COLUMN` in
SQL Server).
### c. The department wants to clear all the data from the 'Employee' table but keep the
table structure intact. Which SQL command should they use from the following options?
The correct option to clear all the data from the `Employee` table while keeping the table
structure intact is:
```python
def count_my_words_in_file(filename):
try:
with open(filename, 'r') as file:
content = file.read()
return count_my
except FileNotFoundError:
return "The file was not found."
# Usage
filename = "STORY.TXT"
result = count_my_words_in_file(filename)
print(f"Count of 'My' in file: {result}")
Q.29 Certainly! Here’s a concise summary of text files, binary files, and CSV files: 3
```python
def count_lines_starting_with_digit(filename):
try:
with open(filename, 'r') as file:
# Initialize line count
count = 0
return count
except FileNotFoundError:
return "The file was not found."
except Exception as e:
return f"An error occurred: {e}"
# Usage
filename = "Diary.txt"
result = count_lines_starting_with_digit(filename)
print(f"Number of lines starting with a digit in the file: {result}")
SECTION D
Q.31 LAN, MAN, WAN, and PAN are acronyms that refer to different types of networks, distinguished 4
primarily by their geographical coverage and intended use. Here's a brief overview of each:
### 1. LAN (Local Area Network)
- **Definition**: A LAN is a network that connects computers and devices within a limited
geographical area, such as a home, school, office, or building.
- **Characteristics**:
- Typically covers a small area, often within a few hundred meters.
- High data transfer rates (typically in the range of 100 Mbps to 10 Gbps).
- Commonly uses Ethernet or Wi-Fi technologies.
- Devices on a LAN can communicate directly with one another, facilitating resource sharing (such
as files and printers).
```python
import struct
# Employee class to represent employee data
class Employee:
def __init__(self, eid, ename, designation, salary):
self.eid = eid
self.ename = ename
self.designation = designation
self.salary = salary
@staticmethod
def from_binary(data):
unpacked_data = struct.unpack('I20s20sf', data)
return Employee(unpacked_data[0], unpacked_data[1].decode('utf-8').strip(b'\
x00').decode('utf-8'),
unpacked_data[2].decode('utf-8').strip(b'\x00').decode('utf-8'),
unpacked_data[3])