CS149 All Midterm2 - REAL

studied byStudied by 2 people
0.0(0)
get a hint
hint

When we execute a C program, CPU runs in ____ mode. a) user b) kernel c) supervisory d) system

1 / 107

Tags and Description

108 Terms

1

When we execute a C program, CPU runs in ____ mode. a) user b) kernel c) supervisory d) system

a) user

New cards
2

In ____ mode, the kernel runs on behalf of the user. a) user b) kernel c) real d) all

b) Kernal

New cards
3

In the layered approach of Operating Systems __________ a) Bottom Layer(0) is the User interface b) Highest Layer(N) is the User interface c) Bottom Layer(N) is the hardware d) Highest Layer(N) is the hardware

b) Highest Layer(N) is the User interface

New cards
4

Less privileged mode is often referred as A.User Mode B.Control Mode C.Kernel Mode D.System Mode

a) User Mode

New cards
5

What statement concerning privileged instructions is considered false? A) They may cause harm to the system. B) They can only be executed in kernel mode. C) They cannot be attempted from user mode. D) They are used to manage interrupts.

c) They cannot be attempted from user mode

New cards
6

How can the software run protected functions in kernel mode? a) Sending signals to CPU through bus b) Executing a special operation called system call (trap) c) Executing a special program called system program d) Executing a special program called interrupt trigger program

b) Executing a special operation called system call (trap)

New cards
7

Switching the CPU to another Process requires to save state of the old process and loading new process state is called as __________. A. Process Blocking B. Context Switch C. Time Sharing D. None of the above

b) Context Switch

New cards
8

What is true about the timer interrupt? 1.The OS sets up a hardware timer that generate an interrupt every N milliseconds. 2.That interrupt is delivered to the kernel and user-code is interrupted. 3.The CPU knows what code to run when the timer interrupt occurs. The CPU scheduler will run next to schedule another process. 4.All of the above

4(D) all of the above 1.It works like any other hardware interrupt. For example your disk will force a switch to the kernel when it has completed an IO.

New cards
9

A fork system call will fail if A. The previously executed statement is also a fork call B. The limit on the maximum number of processes in the system would be exceeded. C. The limit on the maximum number of processes that can be under execution by a single user would be exceeded. D. Both (b) & (c)

D. Both (b) & (c)

New cards
10

The PID of the kernel process is A. Undefined B. 0 C. 1 D. 3

B

PID of the kernel process is 0. There are two tasks with especially distinguished process IDs: swapper or sched has process ID 0 and is responsible for paging, and is actually part of the kernel rather than a normal user-mode process. Process ID 1 is usually the init process primarily responsible for starting and shutting down the system. The process identifier (PID) is a number used by most operating system kernels such as those of UNIX, mac OS and Microsoft windows to uniquely identify an active process. Process 0 is a special process that is created when the system boots; after forking a child process (process 1), process 0 becomes the swapper process.

New cards
11

How does the OS communicate to an application process? A)Signals B)Bus C)Hardware interrupts D)Ethernet

A APUE: Signals are a technique used to notify a process that some condition has occurred. For example, if a process divides by zero, the signal whose name is SIGFPE (floating-point exception) is sent to the process. The process has three choices for dealing with the signal.

  1. Ignore the signal. This option isn't recommended for signals that denote a hardware exception, such as dividing by zero or referencing memory outside the address space of the process, as the results are undefined.

  2. Let the default action occur. For a divide-by-zero condition, the default is to terminate the process.

  3. Provide a function that is called when the signal occurs (this is called ''catching'' the signal). By providing a function of our own, we'll know when the signal occurs and we can handle it as we wish.

Many conditions generate signals. Two terminal keys, called the interrupt key— often the DELETE key or Control-C—and the quit key—often Control-backslash—are used to interrupt the currently running process. Another way to generate a signal is by calling the kill function. We can call this function from a process to send a signal to another process. Naturally, there are limitations: we have to be the owner of the other process (or the superuser) to be able to send it a signal.

New cards
12

What best describes a signal? a)any hardware device generated interrupt b) software or hardware generated event, mediated by kernel c) any interrupt always generated by a user d) none of the mentioned

B https://stackoverflow.com/questions/45485093/signal-vs-exceptions-vs-hardware-interrupts-vs-traps

New cards
13

A (harware-generated) interrupt breaks the execution of instructions and diverts its execution to a) Interrupt handler routine b) Counter word register c) Execution unit d) control unit

a) Interrupt handler routine

New cards
14

What is the name of the signal handler function in this code? A)sig_int B)signal C)err_sys D)main

#include "apue.h" #include <sys/wait.h> static void sig_int(int); int main(void) { char buf[MAXLINE]; pid_t pid; int status;

if(signal(SIGINT, sig_int) == SIG_ERR){ err_sys("signal error"); }

printf("%% "); while(fgets(buf, MAXLINE, stdin) != NULL) { if(buf[strlen(buf) - 1] == '\n') { buf[strlen(buf) - 1] == 0; }

if((pid = fork()) < 0) { err_sys("fork error"); } else if (pid == 0) { execlp(buf, buf, (char *)0); err_ret("couldn't execute: %s", buf); exit(127); }

if ((pid = wait(pid, &status, 0)) < 0) { err_sys("waitpid error"); } printf("%% "); } exit(0);

} void sig_int (int signo) { printf("interrupt\n%% "); }

A Signals are a technique used to notify a process that some condition has occurred. For example, if a process divides by zero, the signal whose name is SIGFPE (floating-point exception) is sent to the process. The process has three choices for dealing with the signal.

  1. Ignore the signal. This option isn't recommended for signals that denote a hardware exception, such as dividing by zero or referencing memory outside the address space of the process, as the results are undefined.

  2. Let the default action occur. For a divide-by-zero condition, the default is to terminate the process.

  3. Provide a function that is called when the signal occurs (this is called ''catching'' the signal). By providing a function of our own, we'll know when the signal occurs and we can handle it as we wish. Many conditions generate signals. Two terminal keys, called the interrupt key— often the DELETE key or Control-C—and the quit key—often Control-backslash—are used to interrupt the currently running process. Another way to generate a signal is by calling the kill function. We can call this function from a process to send a signal to another process. Naturally, there are limitations: we have to be the owner of the other process (or the superuser) to be able to send it a signal.

New cards
15
What does the code do?
A)Prints out the signal number
B)Assigns a handler for Ctrl+C (SIGINT) signal
C)Awaits for the user to send a signal
D) All of the above

#include
#include
#include
static void myHandler(int iSig) {
printf("In myHandler with argument %d\n", iSig);
}
int main(void)
{
void (*pfRet) (int);
pfRet = signal(SIGINT, myHandler);
assert(pfRet != SIG_ERR);
printf("Entering an infinite loop\n");
for(;;)
{
;
}

return 0;
}
D) All of the above
#define _GNU_SOURCE /* Use modern handling style */
#include
#include
#include
New cards
16
What does the code do?
A)Prints out the signal number
B)Assigns a handler for many signals
C)Awaits for the user to send a signal
D) All of the above

#include
#include
#include
static void myHandler(int iSig) {
printf("In myHandler with argument %d\n", iSig);
}
int main(void) {
void (*pfRet)(int);
pfRet = signal(SIGHUP, myHandler); /* 1 */
pfRet = signal(SIGINT, myHandler); /* 2 */
pfRet = signal(SIGQUIT, myHandler); /* 3 */
pfRet = signal(SIGILL, myHandler); /* 4 */
pfRet = signal(SIGTRAP, myHandler); /* 5 */
pfRet = signal(SIGABRT, myHandler); /* 6 */
pfRet = signal(SIGBUS, myHandler); /* 7 */
pfRet = signal(SIGFPE, myHandler); /* 8 */
pfRet = signal(SIGKILL, myHandler); /* 9 */
printf("Entering an infinite loop\n");
for (;;);
return 0;
}

D) All of the above

New cards
17

What problem do you see with this program? A)The file temp.txt could be left open when user does Ctrl+C B)It removes temp.txt C)It removes temp.txt after closing the file D)It returns 0

int main(void) { FILE *psFile; psFile = fopen("temp.txt", "w"); ... fclose(psFile); remove("temp.txt"); return 0; }

A) A)The file temp.txt could be left open when user does Ctrl+C Program generates lots of temporary data

  • Stores the data in a temporary file -Must delete the file before exiting

New cards
18

What does the code do? A)It opens a new file temp.txt B)It cleans up the file if user presses Ctrl+C C)It raises a Ctrl+C signal D)All of the above ... static FILE psFile; / Must be global. */ static void cleanup(int iSig) { fclose(psFile); remove("tmp.txt"); exit(EXIT_FAILURE); } int main(void) { void (pfRet)(int); psFile = fopen("temp.txt", "w"); pfRet = signal(SIGINT, cleanup); ... raise(SIGINT); return 0; / Never get here. */ }

D) all of the above

New cards
19
What does the code do?
A)Specifies a handler for SIGINT signal
B)Specifies a handler for any signal
C)Removes the signal handlers
D)None of the above

#include
#include
#include
static void myHandler(int iSig) {
printf("In myHandler with argument %d\n", iSig);
}
int main(void) {
int iRet;
struct sigaction sAction;
sAction.sa_flags = 0;
sAction.sa_handler = myHandler;
sigemptyset(&sAction.sa_mask);
iRet = sigaction(SIGINT, &sAction, NULL);
assert(iRet == 0);

printf("Entering an infinite loop\n");
for (;;) ;
return 0;
}

A) Specifies a handler for SIGINT signal

New cards
20

____ is the number of processes that are completed per time unit. A) CPU utilization B) Response time C) Turnaround time D) Throughput

D) Throughput

New cards
21

____ is how long it took to execute a process from the time of submission to completion. A) CPU utilization B) Response time C) Turnaround time D) Throughput

C) Turnaround time

New cards
22

____ is how long it took for a process from the time of submission to produce the first response. A) CPU utilization B) Response time C) Turnaround time D) Throughput

B) Response time

New cards
23

Which of the following is true of preemptive scheduling? A) It requires a timer. B) A process keeps the CPU until it releases the CPU either by terminating or switching to waiting/blocked state. C) It incurs a cost associated with access to shared data. D) A process switches from the running state to the ready state when an interrupt occurs.

B is more common in case it is waiting for I/O to complete. D also has sense (relinguishing CPU forcibly because of a timer interrupt)

New cards
24

The ____ scheduling algorithm is more suitable for time-sharing systems. A) SJF B) FCFS (FIFO) C) RR D) STCF

C) RR

New cards
25

Process are classified into different groups in ____________ a) shortest job scheduling algorithm b) round robin scheduling algorithm c) priority scheduling algorithm d) multilevel feedback queue scheduling algorithm

d) multilevel feedback queue scheduling algorithm

New cards
26

Which of the following is true of multilevel feedback queue scheduling? A) Processes can move between queues. B) Each queue has its own scheduling algorithm. C) A queue cannot have absolute priority over lower-priority queues. D) It is the most general CPU-scheduling algorithm.

a) A) Processes can move between queues.

New cards
27

In multilevel feedback scheduling algorithm ____________ a) a process can move to a different classified ready queue b) classification of ready queue is permanent c) processes are not classified into groups d) none of the mentioned

a) a process can move to a different classified ready queue

New cards
28

How many read/write heads are there per platter? a.1 b.2 c.3 d.4

2 b

New cards
29

The "geometry" specification of a drive consists of a. Tracks b. Heads c. Sectors/track d. Cylinders e. Total sectors

b, c, and d

b. Heads c. Sectors/track d. Cylinders https://www.brainbell.com/tutors/A+/Hardware/_Geometry.htm

●Head: If a hard disk drive has four platters, it can have up to eight heads. ●Sectors are like pie slices. Each arc is a sector and holds 512 bytes of data. ●Data is stored in circular paths on the surface of each head.

New cards
30

Sectors are typically a.1.2 megabytes b.256 kilobytes c.512 bytes d.512 kilobytes e.1024 bytes

c.512 bytes

New cards
31

A file is scattered into pieces across surface of a disk, when it is A.compressed B.archived C.defragmented D.fragmented

D.fragmented

New cards
32

How often would you use a disk defragmenter? a.Twice a day b.Once a day c.Once a week d.Once a month

d.Once a month

New cards
33

Division of a hard disk into separate areas of data is called a.Clustering b.Formatting c.Sectioning d.Fragmenting e.Partitioning

e.Partitioning

New cards
34

Partition tables on a hard disk drive may contain: a. d and e b. Two primary partitions and two extended partitions c.Unlimited partitions d.Up to four primary partitions e.Three primary partitions and one extended partition

A: D e

d.Up to four primary partitions e.Three primary partitions and one extended partition

New cards
35

"Volumes" are a.Groups of cylinders b.Partitions c.Master boot records d.Usually assigned a unique identifier, such as a drive letter or folder e.Groups of folders

B d "Volumes" generally has several meanings

b. Partitions d. Usually assigned a unique identifier, such as a drive letter or folder

New cards
36

All primary partitions must have a.An operating system b.An entry in the partition table c.Logical drives d.A corresponding extended partition e.A unique identifier such as a drive letter or folder

b. An entry in the partition table and

e. A unique identifier such as a drive letter or folder

New cards
37

Partitioning a hard drive allows one to a.Increase disk space b.Install multiple operating systems on it c.Proceed with logical partitioning d.Share drive letters e.Encrypt files

b.Install multiple operating systems on it

and

c.Proceed with logical partitioning

New cards
38

Where is the boot sector located on a hard disk drive? a.First sector b.Second sector c.Last sector d.In the cache e.The file allocation table

a.First sector

New cards
39

The boot sector on a hdd contains a.The master boot record b.Speadsheets c.A copy of system BIOS d.Games e.The partition table

a.The master boot record

e.The partition table

New cards
40

The first sector of the hard drive is called a.The first partition b.Cylinder 0 c.The boot sector d.The partition table e.The stripe

c.The boot sector

New cards
41

The boot sector in a basic disk contains a.The master boot record and file allocation table b.The master boot record and partition table c.BIOS and file allocation table d.The partition table and file allocation table e.Boot.ini

b.The master boot record and partition table

New cards
42

Filenames in UNIX are case-sensitive. a) True b) False

a) True

New cards
43

The most common file type is ___ a) ordinary (data) file b) directory file c) device file d) ordinary file and directory file

A Explanation: The most common file type is an ordinary file or a regular file. It contains data as a stream of characters. Ordinary files are also of two types, text file and binary file.

New cards
44

What is a directory file? a) a directory containing data b) a directory containing details of the files and subdirectories it contains c) a directory contains files d) a directory containing data and files

B Explanation: A directory file contains no data but some details of the subdirectories and files that it contains. Directory files contain an entry for every file and subdirectory in it and each entry has some necessary information regarding files and subdirectories.

New cards
45

When we log in, the UNIX places us in a directory, called ______ directory a) home b) main c) parent d) current

a) home

New cards
46

The root directory in UNIX-like OSs is represented by ___ a) b) / c) * d) $

b) /

New cards
47

UNIX treats everything as a file. a) True b) False

A Explanation: All physical devices such as printers, hard disk are treated as files by the UNIX system. Even the kernel, shell and main memory is treated as a file by UNIX operating system.

New cards
48

UNIX imposes no rule for framing filename extensions - but software applications may impose rules (e.g. C compiler expects source code filenames to end with .c). a) True b) False

A Explanation: UNIX imposes no rules for framing filename extensions. For example, a shell script doesn't need to have the .sh implication, even though it helps in identification. In all cases, it's the application that imposes the restriction. For example, C compiler expects C program filenames to end with .c .

New cards
49

What is a buffer? a. A section of memory used as a temp staging area for input or output data. b. The cable that connects a data source to the bus. c. Any stream that deals with IO. d. A file that contains binary data.

a. A section of memory used as a temp staging area for input or output data.

New cards
50

Buffer cache helps to a) Store data b) Improved read/write performance c) Allocate memory d) None of the mentioned

B https://lwn.net/Articles/457667/

New cards
51

In UNIX, the file name and file size are stored in the file itself. a) True b) False

B Explanation: A UNIX file's size is not stored in the file, nor its name. All this information is stored separately in a separate area of hard disk.

New cards
52

Smallest addressable storage unit on hard disk, typically 512 bytes

  1. FAT

  2. bit

  3. Sector

  4. MBR

  1. Sector

New cards
53

1,000,000,000 bytes i. Gigabyte ii. Kilobytes iii. Megabyte iv. Terabyte

i. Gigabyte

New cards
54

Independent organizational structure that allows the user to store and retrieve data

  1. File System

  2. Files

  3. File Slack

  4. File System Area

  1. File System

New cards
55

Group of sectors 1.Sector 2.Acquire 3.File 4.Cluster

Answer: 4. Cluster

explanation: 2.Number of Sectors per cluster can range between 1 and 64 3.Sectors per cluster established when the volume is formatted 4.Windows will usually set 8 sectors per cluster which would give a cluster size of 4096

New cards
56

Are written at the cluster level 1.Files 2.Byte 3.Bit 4.FAT

Answer: 1. Files

explanation: 2.Two files can not be allocated to the same cluster at the same time3.If a file is larger than a single cluster it will use multiple clusters in the volume4.When using multiple clusters they may be contiguous or non-contiguous5.Often have a physical size and logical size

New cards
57

A hard drive has 1 sector (512 bytes) per cluster, and we know that the data area begins in sector 33. Suppose a certain file begins in cluster number 5. Which disk sector contains the beginning of the file? A)38 B)33 C)5 D)0

A)38

New cards
58

A certain hard drive has 4 KB per cluster, and we know that the data area begins in sector 100. If a particular file begins in cluster 10, which disk sectors are used by the cluster? A)180-187 B)180-183 C)100-107 D)110-117

A)180-187

New cards
59

This is a table that contains information about the clusters that are set up by the file system. Acts as a roadmap for the operation system to find all of the clusters in a file i. File Name Area ii. File to File iii. File Allocation Table iv. File System Area

iii. File Allocation Table

New cards
60

Collection of addressable sectors that an Operating System or application can use for that data storage 1.Nibble 2.Volume 3.Files 4.byte

Answer 2.Volume

explain: 1.Hard disk is a volumeMultiple smaller volumes can be combined o create a larger volume

New cards
61

With the Windows FAT filesystem, a larger cluster size means

  1. less internal fragmentation

  2. larger disk partitions can be handled

  3. less external fragmentation

  4. more disk I/O operations per cluster

  5. none of the above

Answer: 3, 4 3. less external fragmentation 4. more disk I/O operations per cluster

explanation: External fragmentation also occurs in file systems as many files of different sizes are created, change size, and are deleted. The effect is even worse if a file which is divided into many small pieces is deleted, because this leaves similarly small regions of free spaces. https://en.wikipedia.org/wiki/Fragmentation_(computing)

●Both 3 and 4 make sense, so I checked them both on the slide.When you have smaller cluster size, it means files will be in many small pieces scattered all over the diskand as you delete files there will be more small clusters (blocks) that are free for use. Thus more external fragmentation.Larger cluster size -> less external fragmentationThe larger cluster size also means more disk I/O since there are more sectors to move between disk and memory.Larger cluster size -> more internal fragmentation because a file with a small size (e.g. just 1 byte) will still take a complete cluster (block), effectively wasting a lot of storage space.

New cards
62

Hard links and symbolic links are the same a) TRUE b) FALSE

B: Symlinks have diff inodenum, hardlinks have same inodenum. If remove orig file symlinks broken, hardlinks not broken due to link_count. Symlinks can be to directories, hardlinks only to regular files. Symlinks can traverse partitions, hardlinks only to the same partition (inodenums are specific to a part)

New cards
63

How to make a filesystem?

🞐After you partition a hard disk, use mkfs tool to make a file system 🞐Write an empty file system, starting with a root directory, on to a disk partition. ⬥Input: 🞆A device (such as a disk partition, e.g., /dev/sda1) 🞆A file system type (e.g., ext3) prompt> mkfs -t ext3 /dev/sdb1 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux prompt> file -sL /dev/sdb1 /dev/sdb1: Linux rev 1.0 ext3 filesystem data (large files)

New cards
64

How to Mount a file system?

🞐mount() ⬥Take an existing directory as a target mount point. ⬥Paste a new file system onto the directory tree at that point. ⬥Examples: prompt> sudo mount /dev/sdd1 /media/usb

prompt> sudo mount /dev/sdb1 /mnt/media 🞆The pathname /media/usb and /mnt/media/ now refer to the roots of the newly-mounted directories.

New cards
65

The directory /media is the a) mount point for removable media b) mount point for filesystem c) mount point for removable media & filesystem d) none of the mentioned

a) mount point for removable media

New cards
66

cat /sys/block/sda/queue/hw_sector_size

512

What does it show? a.Total and free memory b.Number of running processes c.Hard Disk Sector size d.Number of open files

c.Hard Disk Sector size

Everything is a file in UNIX C

New cards
67

cat /proc/meminfo

MemTotal: .... MemFree: .... What does it show? a.Total and free memory b.Number of running processes c.Hard Disk Sector size d.Number of open files

a.Total and free memory

Everything is a file in UNIX A

New cards
68

Which languages necessarily need heap allocation in the run time environment? A) Those that support recursion B) Those that use dynamic scoping C) Those that use global variables D) Those that allow dynamic data structures

D) Those that allow dynamic data structures

New cards
69

Consider the following program. Where are i, j, k stored in memory? ____________ int i; int main() { int j; int *k = (int *)malloc(sizeof(int)); }

________ A) i, j and *k are stored in stack segment B) i and j are stored in stack segment. *k is stored on heap. C) i is stored in uninitialized part of data segment, j, k are stored in stack segment. *k is stored on heap. D) j is stored in uninitialized part of data segment, i is stored in stack segment. *k is stored on heap.

C) i is stored in uninitialized part of data segment, j, k are stored in stack segment. *k is stored on heap.

New cards
70

We use malloc and calloc for A) Dynamic memory allocation B) Static memory allocation C) Both dynamic and static memory allocation D) None of the above

A) Dynamic memory allocation _____ A Both Calloc() and Malloc() are used for dynamic memory allocation. calloc() gives you a zero-initialized buffer, while malloc() leaves the memory uninitialized.

New cards
71

We use malloc and calloc for A) Dynamic memory allocation int S=100; int *a = (int ) malloc(Ssizeof(int)); B) Static memory allocation int[] a = int[100]; C) Both dynamic and static memory allocation D) None of the above

Look at example written on side of answer options:

A) Dynamic memory allocation _____ A Both Calloc() and Malloc() are used for dynamic memory allocation. calloc() gives you a zero-initialized buffer, while malloc() leaves the memory uninitialized.

New cards
72

What is the return type of malloc() or calloc()? A)void * B)Pointer of allocated memory type C)void ** D)int *

A)void * _______ A malloc() and calloc() return void *. you may get warning in C if you don't type cast the return type to appropriate pointer.

New cards
73
What is the problem with this code?
_______
#include
int main()
{
int *p = (int *)malloc(sizeof(int));
p = NULL;
free(p);
}
________
A) Compiler Error: free can't be applied on NULL pointer
B) Memory Leak
C) Dangling Pointer
D) The program may crash as free() is called for NULL pointer.

B) Memory Leak ______ B free() can be called for NULL pointer, so no problem with free function call. The problem is memory leak, p is allocated some memory which is not freed, but the pointer is assigned as NULL. The correct sequence should be following: free(p); p = NULL;

New cards
74

Which languages necessarily need heap allocation in the run time environment? A) Those that support recursion B) Those that use dynamic scoping C) Those that use global variables D) Those that allow dynamic data structures

D) Those that allow dynamic data structures

New cards
75

Consider the following program. Where are i, j, k stored in memory? ______________ int i; int main() { int j; int *k = (int *) malloc(sizeof(int)); }

A) i, j and *k are stored in stack segment B) i and j are stored in stack segment. *k is stored on heap. C) i is stored in uninitialized part of data segment, j, k are stored in stack segment. *k is stored on heap. D) j is stored in uninitialized part of data segment, i is stored in stack segment. *k is stored on heap.

C) i is stored in uninitialized part of data segment, j, k are stored in stack segment. *k is stored on heap.

New cards
76

We use malloc and calloc for A) Dynamic memory allocation B) Static memory allocation C) Both dynamic and static memory allocation D) None of the above

A) Dynamic memory allocation _____ A Both Calloc() and Malloc() are used for dynamic memory allocation. calloc() gives you a zero-initialized buffer, while malloc() leaves the memory uninitialized.

New cards
77
What is the output of this program?
#include
#include
________
int main()
{
int *ptr;
*ptr = 10;
*ptr = 20;
printf("%d\n",*ptr);
return 0;
}
_______
A)10
B)20
C)Segmentation fault
D)None of the above

C The segmentation fault occurs because memory for the pointer has not been allocated in this program.

New cards
78
What is the output of this program?
#include
#include
______
int main()
{
int *ptr1, *ptr2;
ptr1 = (int *) malloc(4);
*ptr1 = 10;
*ptr2 = free(ptr1);
printf("%d\n",*ptr2);
return 0;
}
_____
A)10
B)It will print the address in ptr1
C)It will print the address in ptr2
D)It will give an error

D)It will give an error ______ D The free() function returns no value

New cards
79
What is the problem with following code?
________
#include
int main()
{
int *p = (int *)malloc(sizeof(int));
p = NULL;
free(p);
}
_______
A) Compiler Error: free can't be applied on NULL pointer
B) Memory Leak
C) Dangling Pointer
D) The program may crash as free() is called for NULL pointer.

B free() can be called for NULL pointer, so no problem with free function call. The problem is memory leak, p is allocated some memory which is not freed, but the pointer is assigned as NULL. The correct sequence should be following: free(p); p = NULL;

New cards
80

What is the output? # include<stdio.h> # include<stdlib.h>

void fun(int a) { a = (int)malloc(sizeof(int)); }

int main() { int *p; fun(p); *p = 6; printf("%dn",*p); return(0); } _______ A) May not work B) Works and prints 6

A fun() makes a copy of the pointer, so when malloc() is called, it is setting the copied pointer to the memory location, not p. p is pointing to random memory before and after the call to fun(), and when you dereference it, it will crash. If you want to add memory to a pointer from a function, you need to pass the address of the pointer (ie. double pointer).

New cards
81

Which of the following is/are true A) calloc() allocates the memory and also initializes the allocates memory to zero, while memory allocated using malloc() has random data. B) malloc() can be used to get the same effect as calloc(). C) calloc() takes two arguments, but malloc takes only 1 argument. D) Both malloc() and calloc() return 'void *' pointer. E) All of the above

E) All of the above

New cards
82
What is the output of this program?
#include
#include

int main()
{
int *ptr;
ptr = (int *)calloc(1,sizeof(int));
if (ptr != 0)
printf("%d\n",*ptr);
return 0;
}
________
A)0
B)-1
C)Garbage value
D)none of the mentioned

A)0

New cards
83
This program will allocate the memory of ___ bytes for pointer "ptr".
#include
#include

int main()
{
int *ptr;
ptr = (int*)malloc(sizeof(int)*4);
ptr = realloc(ptr,sizeof(int)*2);
return 0;
}
_________
A)2
B)4
C)8
D)none of the mentioned

C 8

New cards
84

Consider the following three C functions: _____ [PI] int * g (void) { int x= 10; return (&x); } _____ [P2] int * g (void) { int * px; *px= 10; return px; } _______ [P3] int *g (void) { int *px; px = (int *) malloc (sizeof(int)); *px= 10; return px; } _______ Which of the above three functions are likely to cause problems with pointers? A)Only P3 B)Only P1 and P3 C)Only P1 and P2 D)P1, P2 and P3

C)Only P1 and P2

C In P1, pointer variable x is a local variable to g(), and g() returns pointer to this variable. x may vanish after g() has returned as x exists on stack. So, &x may become invalid. In P2, pointer variable px is being assigned a value without allocating memory to it. P3 works perfectly fine. Memory is allocated to pointer variable px using malloc(). So, px exists on heap, it's existence will remain in memory even after return of g() as it is on heap.

New cards
85
What is the output of this program?
#include
#include

int main()
{
int ret;
int *ptr;
ptr = (int *)malloc(sizeof(int)*10);
free(ptr);
free(ptr);
return 0;
}
_______
A)it will print nothing
B)it will give segmentaion fault
C)undefined behaviour
D)none of the mentioned

C)undefined behaviour

New cards
86
What is the output of this program?
#include
#include

int main()
{
int *ptr;
ptr = (int *)malloc(sizeof(int));
printf("%d\n",*ptr);
return 0;
}
_______
A)4
B)-1
C)undefined
D)none of the mentioned

C undefined

https://www.sanfoundry.com/tricky-buggy-questions-answers-malloc-free/

New cards
87

Which of the following is true of compaction? A) It can be done at assembly, load, or execution time. B) It is used to solve the problem of internal fragmentation. C) It cannot shuffle memory contents. D) It is possible only if relocation is dynamic and done at execution time.

D) It is possible only if relocation is dynamic and done at execution time.

New cards
88

_________ allocates the largest hole (free fragment) available in the memory. a.Best Fit b.Worst Fit c.First Fit d.None of the above

b.Worst Fit

New cards
89

_________ allocates the smallest hole (free fragment) available in the memory. a.Best Fit b.Worst Fit c.First Fit d.None of the above

a.Best Fit

New cards
90

_____ is the dynamic storage-allocation algorithm which results in the smallest leftover hole in memory. 1.A) First fit 2.B) Best fit 3.C) Worst fit 4.D) None of the above

2.B) Best fit

New cards
91

_____ is the dynamic storage-allocation algorithm which results in the largest leftover hole in memory. 1.A) First fit 2.B) Best fit 3.C) Worst fit 4.D) None of the above

3.C) Worst fit

New cards
92

The problem of fragmentation arises in ________. a.Static storage allocation b.Stack allocation storage c.Stack allocation with dynamic binding d.Heap allocation

d.Heap allocation

New cards
93

Which of the following memory allocation scheme suffers from External fragmentation? a.Best-fit b.Worst-fit c.First-fit d.All of the above

d.All of the above D Mostly best-fit, though suffers

New cards
94

With paging there is no ________ fragmentation. a) internal b) external c) either type of d) none of the mentioned

b) external

New cards
95

Logical memory is broken into blocks of the same size called _________ a) frames b) pages c) backing store d) none of the mentioned

b) pages

New cards
96

Physical memory is broken into fixed-sized blocks called ________ a) frames b) pages c) backing store d) none of the mentioned

a) frames

New cards
97

Every address generated by the CPU is divided into two parts. They are ____________ a) frame bit & page number b) page number & page offset c) page offset & frame bit d) frame offset & page offset

b) page number & page offset

New cards
98

The _____ table contains the base address of each page in physical memory. a) process b) memory c) page d) frame

c) page

New cards
99

For every process there is a __________ a) page table b) copy of page table c) pointer to page table d) all of the mentioned

a) page table

New cards
100

The operating system maintains a ______ table that keeps track of how many frames have been allocated, how many are there, and how many are available. a) page b) mapping c) frame d) memory

c) frame

New cards

Explore top notes

note Note
studied byStudied by 9 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 8 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 10 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 3 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 36 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 9 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 182 people
Updated ... ago
5.0 Stars(1)
note Note
studied byStudied by 4 people
Updated ... ago
5.0 Stars(1)

Explore top flashcards

flashcards Flashcard92 terms
studied byStudied by 5 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard23 terms
studied byStudied by 2 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard42 terms
studied byStudied by 6 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard28 terms
studied byStudied by 295 people
Updated ... ago
5.0 Stars(2)
flashcards Flashcard100 terms
studied byStudied by 9 people
Updated ... ago
5.0 Stars(5)
flashcards Flashcard76 terms
studied byStudied by 17 people
Updated ... ago
5.0 Stars(1)
flashcards Flashcard153 terms
studied byStudied by 3 people
Updated ... ago
4.0 Stars(1)
flashcards Flashcard256 terms
studied byStudied by 175 people
Updated ... ago
5.0 Stars(3)