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

Worksheet-1

The document contains a worksheet for a student named Deepanshu Pal, covering topics in C programming, C++ programming, data structures, Unix, DBMS, operating systems, SQL, and computer networks. It includes code snippets, explanations of concepts, and examples related to each topic. The document serves as a comprehensive review of programming and database management principles.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Worksheet-1

The document contains a worksheet for a student named Deepanshu Pal, covering topics in C programming, C++ programming, data structures, Unix, DBMS, operating systems, SQL, and computer networks. It includes code snippets, explanations of concepts, and examples related to each topic. The document serves as a comprehensive review of programming and database management principles.

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 PDF, TXT or read online on Scribd
You are on page 1/ 14

Worksheet – 1

Name. : Deepanshu Pal

Reg No. : RA2211033010067

Branch. : CSE SWE

Sec on : 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++) {

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

return 0;

Q2)

The div() func on in C is used to perform integer division and return both the quo ent and
remainder in a single opera on. It is de ned in the stdlib.h header.

#include <stdio.h>

#include <stdlib.h>

int main() {

int numerator = 20, denominator = 3;

div_t result = div(numerator, denominator);

prin ("Quo ent: %d\n", result.quot);


ti
tf
tf
ti
ti
ti
fi
ti
prin ("Remainder: %d\n", result.rem);

return 0;

Q3)

#include <stdio.h>

int main() {

int day, month, year;

prin ("Enter the date in dd-mm-yy format: ");

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

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

return 0;

Q4)

#include <stdio.h>

int main() {

oat num = 23.34568734;

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

return 0;
fl
tf
tf
tf
tf
}

Q5)

In C, you can dynamically allocate memory for an array using the malloc(), calloc(), or
realloc() func ons from the stdlib.h library. Dynamic memory alloca on allows you to
allocate memory at run me, giving exibility 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;

}
ti
ti
fl
ti
};

int main() {

Date today;

today.setDate(26, 2, 2025);

today.displayDate();

return 0;

Q2)

The this pointer cannot be used inside a sta c member func on in C++. The this pointer
refers to the current object of a class. However, sta c member func ons belong to the class
itself rather than any par cular object. As a result, sta c func ons can be called without
crea ng 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 sta c func on. Consequently, a emp ng to use
this inside a sta c member func on will result in a compila on error. Sta c func ons can
only access sta c data members and other sta c func ons directly. To access non-sta c
members, an explicit object reference is required.

DATA STRUCTURES

Q1)

Inorder Output:

HDBEAFCIGJ

Preorder Output:

ABDHECFGIJ

Postorder Output:

HDEBFIJGCA
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
tt
ti
ti
ti
ti
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 execu on yet.
2. Ready – The process is wai ng in the queue for CPU me to execute.
3. Running – The process is currently being executed by the CPU.
4. Wai ng (Blocked) – The process is wai ng for some resource (like I/O or another
process).
5. Terminated (Zombie) – The process has completed execu on but s ll exists in the
process table un l 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 execu on, alloca ng CPU me.
4. Execu on: The program runs and interacts with system resources ( les, memory,
etc.).
5. Comple on: Once nished, the process exits (exit()) and becomes a zombie un l the
parent collects its exit status.
DBMS

Q1)

A Data Dic onary (or Data Directory) is a repository of metadata (data about data) in a
database. It stores informa on about database objects such as tables, columns, data types,
constraints, indexes, users, and rela onships.

Types of Data Dic onary:

1. Ac ve Data Dic onary – Automa cally updated by the DBMS when any changes
occur (e.g., in MySQL, Oracle).
Passive Data Dic onary – Manually updated by database administrators.
ti
ti
ti
ti
ti
ti
ti
ti
ti
fi
ti
ti
ti
ti
ti
ti
ti
ti
ti
fi
ti
ti
ti
ti
Q2)

ACID proper es ensure reliable database transac ons. They are:

1. Atomicity – "All or Nothing" rule. A transac on 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 transac on is rolled back.
2. Consistency – Ensures data integrity by keeping the database in a valid state before
and a er transac ons.
o Example: If a bank transac on violates balance constraints, the database rolls
back the change.
3. Isola on – Ensures transac ons do not interfere with each other and execute
independently.
o Example: Two users booking the last train cket should not be able to book it
simultaneously.
4. Durability – Once a transac on is commi ed, it remains permanently stored even if
the system crashes.
o Example: A er a successful bank transac on, the money should remain
transferred even if the server goes down.

Opera ng 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 xed number of frames, and it cannot take memory from
other processes.
o Example: Fixed Alloca on – 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 starva on (some processes
may keep losing pages).
ti
ft
ti
ft
ti
ti
fi
ti
ti
ti
ti
ti
ti
tt
ti
ti
ti
ti
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 me-consuming part of disk access.
o Example: If data is on track 40 but the head is at track 10, it takes me to
move to track 40.
• Latency (Rota onal Latency):
o Time taken by the disk to rotate the required sector under the read/write
head.
o Depends on disk RPM (Rota ons Per Minute).
o Example: The sector might be at the other end of the disk, so it takes me to
rotate to the correct posi on.
• Transfer Time:
o Time required to transfer data from the disk to memory once the head is at
the right posi on.
o Example: Once the sector is under the head, the me taken to read and send
it to RAM.

Q3)

Paging is a memory management technique where the OS divides process memory into
xed-sized pages and physical memory into xed-sized frames.
• Why use Paging?
o Eliminates external fragmenta on (unused memory between allocated
spaces).
o Allows non-con guous memory alloca on (pages of a process can be
anywhere in RAM).
How Paging Works?

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


2. RAM is divided into frames (same size as pages).
fi
ti
ti
ti
ti
ti
ti
ti
ti
fi
fi
ti
ti
ti
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 ac ons:

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() func on is used in SQL to nd the di erence 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 so ware developed in VB?

SELECT MAX(SCOST) AS Costliest_So ware_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
Interconnec on) model, the following process occurs:
ti
ti
ft
ti
ft
fi
ff
Encapsula on Process:

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

Step-by-step process:

1. Applica on Layer (Layer 7) → The user interacts with an applica on (e.g., web
browser, email client).
2. Presenta on Layer (Layer 6) → Data is forma ed, encrypted, or compressed if
needed.
3. Session Layer (Layer 5) → Manages session crea on and termina on.
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 des na on 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, op cal, or radio signals
for transmission over the network.
Example (Sending an HTTP request from a web browser):
• Applica on 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, Des na on IP: 8.8.8.8).
• Data Link Layer: Adds MAC header & trailer (e.g., Source MAC: A1:B2:C3:D4:E5:F6,
Des na on MAC: X1:Y2:Z3:W4:V5:U6).
• Physical Layer: Converts the frame into electrical signals or radio waves.
This process is known as Encapsula on and ensures that data reaches its des na on
correctly over the network.

Q2)

· Connec on Type
• TCP (Transmission Control Protocol) is a connec on-oriented protocol, meaning it
establishes a reliable connec on before data transfer begins.
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
tt
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
• UDP (User Datagram Protocol) is connec onless, meaning data is sent without
se ng up a dedicated connec on.
· 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 conges on 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 applica ons where data integrity and reliability are important, such as
web browsing (HTTP/HTTPS), le transfer (FTP), and email (SMTP).
• UDP is used in applica ons where speed is more important than reliability, such as
video streaming, VoIP, gaming, and real- me communica on.
· Header Size
• TCP has a larger header (20-60 bytes) due to extra informa on for reliability and ow
control.
• UDP has a smaller header (8 bytes), making it more e cient for quick data
transmission.
· Flow Control
• TCP supports ow control and conges on control, which helps manage network
tra c and prevent packet loss.
tti
ffi
fl
ti
ti
fi
ti
ti
ti
ti
ti
ffi
ti
ti
fl
• UDP does not have ow control, making it more suitable for high-speed applica ons.

Company Speci c Ques ons

TCS Company Speci c Coding Ques ons


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;

}
fi
fl
fi
ti
ti
ti
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 printPa ern(int n) {

int totalRows = 2 * n - 1;

char le ers[n];

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

le ers[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 << "-";


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

cout << le ers[j];

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

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

cout << "-";

cout << le ers[j];

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

cout << endl;

int main() {

int n;

cin >> n;

printPa ern(n);

return 0;

}
tt
tt
tt

You might also like