0% found this document useful (0 votes)
7 views

Worksheet

The document contains a series of programming exercises and explanations related to C, C++, data structures, Unix, DBMS, operating systems, SQL, computer networks, and company-specific coding questions. It includes code snippets, descriptions of functions, and concepts such as memory management, database properties, and network protocols. Each section addresses specific questions and provides example code to illustrate the concepts discussed.

Uploaded by

upbeatdewdney
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Worksheet

The document contains a series of programming exercises and explanations related to C, C++, data structures, Unix, DBMS, operating systems, SQL, computer networks, and company-specific coding questions. It includes code snippets, descriptions of functions, and concepts such as memory management, database properties, and network protocols. Each section addresses specific questions and provides example code to illustrate the concepts discussed.

Uploaded by

upbeatdewdney
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Worksheet – 1

Name. : Priyanshu K Sinha


Reg No. : RA2211033010015
Branch. : CSE SWE
Section : AB-1
——————————————————————————————————————

C-Programming
Q1)

We can print the contents of the of environment variables in C by

#include <stdio.h>

int main(int argc, char *argv[], char *envp[])

{ for (int i = 0; envp[i] != NULL; i++) {

printf("%s\n", envp[i]);

return 0;

Q2)

The div() function in C is used to perform integer division and return both the quotient and
remainder in a single operation. It is defined in the stdlib.h header.

#include <stdio.h>

#include <stdlib.h>

int main() {

int numerator = 20, denominator = 3;

div_t result = div(numerator, denominator);

printf("Quotient: %d\n", result.quot);


printf("Remainder: %d\n", result.rem);

return 0;

Q3)

#include <stdio.h>

int main() {

int day, month, year;

printf("Enter the date in dd-mm-yy format:

"); scanf("%d-%d-%d", &day, &month,

&year);

printf("Day: %d, Month: %d, Year: %d\n", day, month, year);

return 0;

Q4)

#include <stdio.h>

int main() {

float num = 23.34568734;

printf("Number with 2 decimal places: %.2f\n", num);

return 0;
}

Q5)

In C, you can dynamically allocate memory for an array using the malloc(), calloc(), or
realloc() functions from the stdlib.h library. Dynamic memory allocation allows you to
allocate memory at runtime, giving flexibility when the size of the array isn't known
beforehand.

C++ Programming
Q1)

#include <iostream>

using namespace std;

class Date {

private:

int day, month, year;

public:

void setDate(int d, int m, int y)

{ day = d;

month = m;

year = y;

void displayDate() const {

cout << "Date: " << (day < 10 ? "0" : "") << day << "-"

<< (month < 10 ? "0" : "") << month << "-"

<< year << endl;

}
};

int main() {

Date today;

today.setDate(26, 2, 2025);

today.displayDate();

return 0;

Q2)

The this pointer cannot be used inside a static member function in C++. The this pointer
refers to the current object of a class. However, static member functions belong to the class
itself rather than any particular object. As a result, static functions can be called without
creating an instance of the class. Since there is no object associated with the call, the this
pointer does not exist in the context of a static function. Consequently, attempting to use
this inside a static member function will result in a compilation error. Static functions can
only access static data members and other static functions directly. To access non-static
members, an explicit object reference is required.

DATA STRUCTURES

Q1)

Inorder Output:

HDBEAFCIGJ

Preorder Output:

ABDHECFGIJ

Postorder Output:

HDEBFIJGCA
Unix
Q1)

In Unix, a process can be in one of the following states:

1. New (Created) – The process is being created but has not started execution yet.
2. Ready – The process is waiting in the queue for CPU time to execute.

3. Running – The process is currently being executed by the CPU.


4. Waiting (Blocked) – The process is waiting for some resource (like I/O or
another process).
5. Terminated (Zombie) – The process has completed execution but still exists in
the process table until the parent collects its exit status.
6. Stopped (Suspended) – The process is stopped and can be resumed later (e.g.,
using kill -STOP and kill -CONT).
Q2)

When you run a program (e.g., by typing ./a.out in the terminal), the following steps occur:

1. Fork: The shell creates a child process using the fork() system call.

2. Exec: The child process replaces itself with the new program using exec(), loading
the executable into memory.
3. Process Scheduling: The OS schedules the process for execution, allocating CPU time.

4. Execution: The program runs and interacts with system resources (files,
memory, etc.).
5. Completion: Once finished, the process exits (exit()) and becomes a zombie until
the parent collects its exit status.
DBMS

Q1)

A Data Dictionary (or Data Directory) is a repository of metadata (data about data) in a
database. It stores information about database objects such as tables, columns, data types,
constraints, indexes, users, and relationships.

Types of Data Dictionary:

1. Active Data Dictionary – Automatically updated by the DBMS when any


changes occur (e.g., in MySQL, Oracle).
Passive Data Dictionary – Manually updated by database administrators.
Q2)

ACID properties ensure reliable database transactions. They are:

1. Atomicity – "All or Nothing" rule. A transaction is either fully completed or


not executed at all.
o
Example: If money is transferred from A → B, both debit from A and credit
to B must happen. If one fails, the transaction is rolled back.
2. Consistency – Ensures data integrity by keeping the database in a valid state
before and after transactions.
o
Example: If a bank transaction violates balance constraints, the database
rolls back the change.
3. Isolation – Ensures transactions do not interfere with each other and
execute independently.
o
Example: Two users booking the last train ticket should not be able to book
it simultaneously.
4. Durability – Once a transaction is committed, it remains permanently stored even
if the system crashes.
o
Example: After a successful bank transaction, the money should
remain transferred even if the server goes down.

Operating System
Q1)

Page replacement occurs when a process needs a page that is not in memory, and the
system selects a page to remove.

Local Page Replacement
o
The process replaces pages from its allocated memory only.
o
Each process has a fixed number of frames, and it cannot take memory
from other processes.
o
Example: Fixed Allocation – If a process has 5 frames, it can only
replace pages within those 5 frames.

Global Page Replacement
o
The process can replace any page in memory, even from another process.
o
It improves system performance but may cause starvation (some
processes may keep losing pages).
o
Example: LRU (Least Recently Used), FIFO (First In First Out) – The
system selects a page from all available frames, not just the current
process’s allocated memory.

Q2)

Seek Time:
o
Time taken by the disk’s read/write head to move to the required
track (cylinder).
o
Most time-consuming part of disk access.
o
Example: If data is on track 40 but the head is at track 10, it takes time
to move to track 40.

Latency (Rotational Latency):
o
Time taken by the disk to rotate the required sector under the
read/write head.
o
Depends on disk RPM (Rotations Per Minute).
o
Example: The sector might be at the other end of the disk, so it takes time
to rotate to the correct position.

Transfer Time:
o
Time required to transfer data from the disk to memory once the head is
at the right position.
o
Example: Once the sector is under the head, the time taken to read and
send it to RAM.

Q3)

Paging is a memory management technique where the OS divides process memory into
fixed-sized pages and physical memory into fixed-sized frames.

Why use Paging?
o
Eliminates external fragmentation (unused memory between
allocated spaces).
o
Allows non-contiguous memory allocation (pages of a process can
be anywhere in RAM).
How Paging Works?

1. Process memory is divided into pages (fixed-size, e.g., 4KB each).

2. RAM is divided into frames (same size as pages).


3. When a process runs, its pages are loaded into available frames.

4. A Page Table stores mappings between pages and frames.


5. If a required page is not in RAM (page fault), the OS brings it from the disk.

SQL
Q1)

This query performs the following actions:

1. Retrieves the SAL (Salary) column from the EMP table.

2. Retrieves the COMM (Commission) column but uses NVL(COMM, 0), which
replaces any NULL values in COMM with 0.
3. Adds the Salary (SAL) and the Commission (COMM) to compute the total earnings
for each employee.
Q2)

The DATEDIFF() function is used in SQL to find the difference between two dates. SELECT

DATEDIFF(interval, start_date, end_date);

1. Display the details of packages for which the development cost has been

recovered. SELECT *

FROM SOFTWARE

WHERE (SOLD * SCOST) >= DCOST;

2. What is the price of the costliest software developed in

VB? SELECT MAX(SCOST) AS Costliest_Software_Price

FROM SOFTWARE

WHERE DEVIN = 'VB';

Computer Networks
Q1)

As a data packet moves from the upper to lower layers in the OSI (Open Systems
Interconnection) model, the following process occurs:
Encapsulation Process:

Each layer adds its own header (and sometimes trailer) to the data received from the layer
above before passing it down to the next layer.

Step-by-step process:

1. Application Layer (Layer 7) → The user interacts with an application (e.g.,


web browser, email client).
2. Presentation Layer (Layer 6) → Data is formatted, encrypted, or compressed
if needed.
3. Session Layer (Layer 5) → Manages session creation and termination.

4. Transport Layer (Layer 4) → Adds a TCP/UDP header to ensure reliable (TCP) or


fast (UDP) delivery.
5. Network Layer (Layer 3) → Adds an IP header with source and destination
IP addresses.
6. Data Link Layer (Layer 2) → Adds a MAC header and trailer (Frame) with
physical addresses.
7. Physical Layer (Layer 1) → Converts the data into electrical, optical, or radio
signals for transmission over the network.
Example (Sending an HTTP request from a web browser):

Application Layer: Sends "GET /index.html" (HTTP request).

Transport Layer: Adds TCP header (e.g., Port 80 for HTTP).

Network Layer: Adds IP header (e.g., Source IP: 192.168.1.2, Destination IP: 8.8.8.8).

Data Link Layer: Adds MAC header & trailer (e.g., Source MAC:
A1:B2:C3:D4:E5:F6, Destination MAC: X1:Y2:Z3:W4:V5:U6).

Physical Layer: Converts the frame into electrical signals or radio waves.
This process is known as Encapsulation and ensures that data reaches its destination
correctly over the network.

Q2)

· Connection Type

TCP (Transmission Control Protocol) is a connection-oriented protocol, meaning
it establishes a reliable connection before data transfer begins.

UDP (User Datagram Protocol) is connectionless, meaning data is sent
without setting up a dedicated connection.
· Reliability

TCP is reliable as it ensures data is delivered correctly and in order. It retransmits
lost packets and waits for acknowledgments.

UDP is unreliable since it does not guarantee data delivery, order, or
retransmission of lost packets.
· Error Checking

TCP performs extensive error checking, acknowledgments, and retransmission
to ensure data integrity.

UDP has minimal error checking and does not request retransmission if data is lost.

· Speed

TCP is slower due to its reliability mechanisms, such as error
checking, acknowledgments, and congestion control.

UDP is faster since it does not wait for acknowledgments or retransmit lost packets.

· Order of Data

TCP ensures data arrives in the correct order using sequence numbers.

UDP does not guarantee the order of data arrival, as packets may arrive out
of sequence.
· Usage

TCP is used for applications where data integrity and reliability are important, such
as web browsing (HTTP/HTTPS), file transfer (FTP), and email (SMTP).

UDP is used in applications where speed is more important than reliability, such
as video streaming, VoIP, gaming, and real-time communication.
· Header Size

TCP has a larger header (20-60 bytes) due to extra information for reliability and
flow control.

UDP has a smaller header (8 bytes), making it more efficient for quick
data transmission.
· Flow Control

TCP supports flow control and congestion control, which helps manage
network traffic and prevent packet loss.

UDP does not have flow control, making it more suitable for high-speed applications.

Company Specific Questions

TCS Company Specific Coding Questions


Q1)

#include <iostream>

#include <vector>

using namespace std;

int longestIncreasingSubsequence(vector<int>& arr)

{ int n = arr.size();

vector<int> lis(n, 1);

for (int i = 1; i < n; i++)

{ for (int j = 0; j < i; j++)

if (arr[i] > arr[j] && lis[i] < lis[j] + 1)

{ lis[i] = lis[j] + 1;

int maxLength = 0; for

(int length : lis) {

maxLength = max(maxLength, length);

return maxLength;

}
int main()

{ int n;

cin >> n;

vector<int> arr(n);

for (int i = 0; i < n; i++)

{ cin >> arr[i];

cout << longestIncreasingSubsequence(arr) << endl;

return 0;

Q2)

#include <iostream>

using namespace std;

void printPattern(int n)

{ int totalRows = 2 * n -

1; char letters[n];

for (int i = 0; i < n; i++)

{ letters[i] = 'a' + i;

for (int i = 1; i <= totalRows; i++) {

int currentLevel = i <= n ? i : totalRows - i + 1;

for (int j = 0; j < n - currentLevel; j++) cout << "-";


for (int j = currentLevel - 1; j >= 0; j--)

{ cout << letters[j];

if (j > 0) cout << "-";

for (int j = 1; j < currentLevel; j++)

{ cout << "-";

cout << letters[j];

for (int j = 0; j < n - currentLevel; j++) cout << "-";

cout << endl;

int main()

{ int n;

cin >> n;

printPattern(n);

return 0;

You might also like