0% found this document useful (0 votes)
26 views6 pages

Assignment 1 Week 1 961

Uploaded by

Vasu Gamers
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)
26 views6 pages

Assignment 1 Week 1 961

Uploaded by

Vasu Gamers
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/ 6

ASSIGNMENT 1

Date: 10/12/24
Name: Barnavo Dey
Reg No: RA2211003011961

ANSWERS

C PROGRAMMING

1. The output for the given code is 0


Here’s a step-by-step explanation of the code:
Initialize Variables: i = 0 and a[3] (an array of size 3).
Assign and Increment: a[i] = i++;
▪ a[0] = 0
▪ i is incremented to 1.
Access Array Element: printf("%d", a[i]);
▪ Now i = 1, so it tries to print a[1].
Issue: a[1] is not initialized, so its value is garbage or random.
Output:
The program may print a random value due to undefined behaviour.

2. The issue with the given code lies in how the multiplication of x and y is handled
before being assigned to z. Here's the explanation:
int x = 3000, y = 2000;
long int z = x * y;
- x and y are declared as int values are limited by the range of the int type
- z is declared as long int, which has a larger range.
When x * y is computed, both x and y are int types. The result of their multiplication is
computed in int type, not long int, which exceeds the maximum range of a 16-bit int

Overflow Occurs: Since x * y exceeds the int range, it causes integer overflow, resulting
in an incorrect value being stored in z. The value stored in z is incorrect
Correct Solution:
To fix this issue, we use explicit casting by putting to long int before the multiplication
int x = 3000, y = 2000;
long int z = (long int)x * y;
Now the multiplication is done in the long int range, preventing overflow, and z will
correctly hold 6,000, 000.

3. The issue is how the strcat is used. Here’s a short explanation:


The strcat function adds one string to the end of another.
char str[] = "Hello"; initializes str as an array of size 6
strcat(str, '!'); is invalid because strcat expects its second argument to be a string (a char
array ending with a null terminator), not a single character.
In this case, '!' is a char, not a valid string, so the code doesn’t compile.

4. The given formulacan be used :


sizeof(array) / sizeof(array[0])
This divides the total size of the array by the size of one element. It gives the number of
elements in the array.
- sizeof(array) gives the total size of the array in bytes.
- sizeof(array[0]) gives the size of one element in the array.
For example:
int arr[5];
printf("%d", sizeof(arr) / sizeof(arr[0]));
// Output will be 5

5. To read data from a memory location using a segment and offset, you can add the
segment and offset to get the actual memory address and then use a pointer to access
the data.
Code Example:
#include <stdio.h>
#include <stdint.h> // For uintptr_t
int main() {
unsigned int segment = 0x1000; // Example segment
unsigned int offset = 0x200; // Example offset
uintptr_t physical_address = (uintptr_t)(segment * 16 + offset);
printf("Simulated physical memory address: 0x%X\n", (unsigned int)physical_address);
unsigned int* ptr = (unsigned int*)malloc(sizeof(unsigned int));
if (ptr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
*ptr = 42; // Example write to memory
printf("Data at allocated memory: %d\n", *ptr);
// Free allocated memory
free(ptr);
return 0;
}
OUTPUT

Explanation:
Segment + Offset: Adds the segment and offset to form the full memory address.
Pointer: The pointer ptr is used to access the data at the specified address.
This approach works in low-level programming but is generally avoided in modern
systems for safety.

C++ PROGRAMMING

1. CODE EXPLANATION:
class Sample {
public:
int *ptr;
Sample(int i) {
ptr = new int(i); // Dynamically allocate memory and store the value i.
}
~Sample() {
delete ptr; // Deallocate memory for the pointer ptr when the object is destroyed
}
void PrintVal() {
cout << "The value is " << *ptr; // Dereference the pointer to print the value
}
};
void SomeFunc(Sample x) {
cout << "Say I am in SomeFunc " << endl;
// x goes out of scope and its destructor is called at the end of the function.
}
int main() {
Sample s1 = 10; SomeFunc(s1); s1.PrintVal(); // Calls the constructor, dynamically allocates memory, and
assigns 10.
// Passes s1 to SomeFunc. This causes a copy of s1 to be made.
// Prints the value stored in s1.
}
OUTPUT
Say I am in SomeFunc
The value is 10

EXPLANATION

First output (inside SomeFunc):


When SomeFunc(s1) is called, it prints Say I am in SomeFunc before the copy of s1
goes out of scope and its destructor is called.
Second output (inside main):
After returning from SomeFunc(), s1.PrintVal() is called, which prints The value is 10
since the original s1 still holds the value 10.

2. The parameter that is automatically added to every non-static member function when it
is called is the this pointer.

Explanation:
The this pointer is an implicit pointer that refers to the current instance of the object.
It is automatically passed to every non-static member function of a class when that
function is called, allowing the function to access and modify the object's non-static
Members.

For example:
class MyClass {
public:
int value;
void setValue(int val) {
this->value = val; // 'this' pointer is used to refer to the object's 'value'
}
};
Here, this->value refers to the value of the current object of type MyClass. You don't need
to explicitly pass this; it is automatically available inside non-static member functions

TECHNICAL QUESTION

Data Structures

1. What is a data structure?

A data structure is a way of organizing and storing data so that it can be accessed and
modified efficiently. Ex: arrays, linked lists, stacks, queues, trees, and graphs.
2. List out the areas in which data structures are applied extensively?

- Databases: Storing and organizing large volumes of data.


- Operating Systems: Managing processes, memory, and file systems.
- Networking: Routing tables, data transmission, etc.
- Compilers: Managing syntax trees, symbol tables.
- Artificial Intelligence: Search algorithms, decision trees.
- Graphics: Storing and manipulating images, polygons.

Unix

1. How are devices represented in UNIX?


Devices in UNIX are represented as files, typically in the /dev directory. Each device is
assigned a file that allows access to the device like any other file in the system (e.g.,
/dev/sda for storage devices).
2. What is 'inode'?
An inode is a data structure in a file system that stores information about a file or a
directory, such as its size, ownership, permissions, and pointers to the data blocks.

DBMS

1. What is a database?
A database is a collection of structured data stored in a systematic way that allows easy
retrieval, insertion, updating, and deletion of data. It is managed by a Database
Management System (DBMS).
2. What is a key? What are different keys in a database?
A key is a field (or a set of fields) used to identify records in a database.
Types of keys include:
- Primary Key: Uniquely identifies each record in a table.
- Foreign Key: A field in one table that uniquely identifies a record in another table.
- Candidate Key: A set of fields that could be used as a primary key.
- Composite Key: A key formed by combining multiple columns to uniquely identify a
record.
- Unique Key: Ensures that all values in a column are distinct.

Operating System

1. Explain the concept of Reentrancy.


Reentrancy is the property of a function or program that allows it to be safely interrupted
and called again ("re-entered") before its previous executions are complete, without
affecting the results. It is commonly used in multi-threaded or interrupt-driven systems.
2. Explain Beladv's Anomaly.
Beladv's anomaly refers to a situation in database normalization where the addition of a
new attribute or modification to a relation causes unexpected behavior or anomalies in the
database design, especially related to dependency or integrity issues.

SQL

1. Which is the subset of SQL commands used to manipulate Oracle Database


structures, including tables?
The subset of SQL commands used for manipulating Oracle database structures is called
Data Definition Language (DDL). This includes commands like CREATE, ALTER,
DROP, and TRUNCATE.
2. What operator performs pattern matching?
The LIKE operator in SQL is used for pattern matching, often with wildcard characters
such as % (any sequence of characters) and _ (a single character).
Computer Networks
1. What are the two types of transmission technology available?
The two types of transmission technologies are:
- Wired Transmission: Uses physical cables (e.g., coaxial cables, fiber optics, twisted
pair cables).
- Wireless Transmission: Uses radio waves or other wireless signals (e.g., Wi-Fi,
Bluetooth, cellular networks).

You might also like