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

Week 2

The document covers various topics in C and C++ programming, including character comparison, data object types, process creation, and linked lists. It also discusses database concepts, UNIX directory structure, and operating system principles like semaphores and thrashing. Additionally, it includes code examples for finding the middle of a linked list and calculating the maximum subarray sum.

Uploaded by

as0876
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)
2 views

Week 2

The document covers various topics in C and C++ programming, including character comparison, data object types, process creation, and linked lists. It also discusses database concepts, UNIX directory structure, and operating system principles like semaphores and thrashing. Additionally, it includes code examples for finding the middle of a linked list and calculating the maximum subarray sum.

Uploaded by

as0876
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/ 5

Ashutosh Sharma

RA2211003011350

Week 2
C Programming

1. How do I compare character data stored at two different memory locations.

- We can compare characters stored at two different memory locations using relational
operators like ==, <, or >.

Eg –
char a = 'A';
char b = 'B';

if (a == b) {
printf("Characters are equal\n");
} else {
printf("Characters are not equal\n");
}

2. Explain the difference between fixed-size and variable-size data objects.

- Fixed-size data objects: Have a predetermined size at compile-time. Example:


Arrays.

Variable-size data objects: Their size can change during runtime. Example:
Dynamically allocated memory using malloc or calloc.

We should use fixed-size data object when the data size is known beforehand, providing
simpler code and lower overhead.

3. What is the purpose of the spawnl() function, and how is it used in a program?

- The spawnl() function is used to create a new process by replacing the current process
image with a new program image.

4. Are the following two statements identical?


char str[6] = "Kicit"; char *str = "Kicit";

- No, they are not identical:


• char str[6]: Allocates a fixed array on the stack, including a null terminator.
• char *str: Declares a pointer to a string literal, which is stored in read-only memory.

5. Is the following code fragment correct?


const int x = 10; int arr[x];

- In C, this code will not work because x is a const but not a compile-time constant. We need
a macro or #define.
C++ Programming
1. Output-
0
0
0
0
0

2. Output-
from base
from base

Technical Questions
Data Structures

1. Parameters in RDBMS, Network Data Model, and Hierarchical Data Model:


o RDBMS:
▪ Tables (relations), columns (attributes), rows (tuples), keys
(primary and foreign keys), constraints, relationships.
o Network Data Model:
▪ Nodes (records), edges (relationships), schema with parent-child
relationships (many-to-many associations).
o Hierarchical Data Model:
▪ Root node, child nodes, parent-child relationship (one-to-many),
levels of hierarchy.
2. Pointer Type for Heterogeneous Linked List in C:
o Use a void* pointer because it can store the address of any data type,
making it suitable for heterogeneous data.

Unix

1. Directory Representation in UNIX:


o UNIX uses a hierarchical tree structure for directories.
o The root directory / is at the top, and all other directories and files are
organized under it.
o Each directory may contain files or other subdirectories.
2. Unix System Calls for I/O:
o Common system calls for I/O in UNIX include:
▪ open(), read(), write(), close(), lseek(), ioctl().

DBMS

1. Primary Key:
o A unique identifier for a record in a table.
o Ensures each row has a unique value and cannot be NULL.
2. Secondary Key:
o A field or combination of fields used for data retrieval.
o It is not unique but helps index and access data efficiently.

Operating System

1. Binary Semaphore:
o A semaphore that takes only two values: 0 and 1.
o Used to manage access to a resource between processes or threads
(mutual exclusion).
2. Thrashing:
o A condition where excessive paging operations occur in a system due to
insufficient memory, leading to reduced performance.

SǪL

1. Operator to Test for Absence of Data:


o Use the IS NULL operator.
2. Command to Execute Contents of a Specified File:
o Use the SOURCE or .\<filename.sql> command (for MySǪL).

Computer Networks

1. Subnet:
o A smaller network within a larger network, created by dividing an IP
network using a subnet mask.
o It improves network management and security.

Company Specific Questions


1-
#include <iostream>
using namespace std;

struct ListNode {
int data;
ListNode* next;
ListNode(int val) : data(val), next(nullptr) {}
};

int findMiddle(ListNode* head) {


ListNode* slow = head;
ListNode* fast = head;
while (fast != nullptr CC fast->next != nullptr) {
slow = slow->next;
fast = fast->next->next;
}
return slow->data;
}

void addNode(ListNode*C head, int val) {


if (!head) {
head = new ListNode(val);
return;
}
ListNode* temp = head;
while (temp->next) temp = temp->next;
temp->next = new ListNode(val);
}

int main() {
ListNode* head = nullptr;
addNode(head, 1);
addNode(head, 2);
addNode(head, 3);
addNode(head, 4);
addNode(head, 5);
cout << "Middle of the list: " << findMiddle(head) << endl;
head = nullptr;
addNode(head, 2);
addNode(head, 4);
addNode(head, 6);
addNode(head, 7);
addNode(head, 5);
addNode(head, 1);
cout << "Middle of the list: " << findMiddle(head) << endl;
return 0;
}
2-
#include <iostream>
#include <vector>
using namespace std;

int maxSubArraySum(vector<int>C arr) {


int maxSum = arr[0];
int currentSum = arr[0];
for (int i = 1; i < arr.size(); i++) {
currentSum = max(arr[i], currentSum + arr[i]);
maxSum = max(maxSum, currentSum);
}
return maxSum;
}

int main() {
vector<int> arr1 = {2, 3, -8, 7, -1, 2, 3};
cout << "Maximum subarray sum: " << maxSubArraySum(arr1) << endl;
vector<int> arr2 = {-2, -4};
cout << "Maximum subarray sum: " << maxSubArraySum(arr2) << endl;
return 0;
}

You might also like