CSC 322 FINAL EXAM

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

How are integer constants expressed using decimal, octal, and hexidecimal?

Decimal: %d (base 10 just numbers) Octal: %o (base 8) Hexadecimal: %x (base 16)

Rewrite the following for loop as a while loop. for (Index = 1;Index < MAX; Index++) { printf("%d\n", Index); Total += Index; }

int Index =1; while(Index<Max){ Printf("%d\n", Index); Total += Index; Index++; }

After a call to uname, what information is stored in the struct utsname pointed to be the parameter?

information identifying the current system

Write a program that creates a second process, and then in both processes outputs the process ID and the owners user ID.

#include <unistd.h> getpid(); getuid();

The following problem could suffer from a divide by zero error. Rewrite it adding an assert statement to prevent the potential error. int i1,i2; scanf("%d %d",&i1,&i2); printf("%d\n:,i1/i2);

#include assert.h assert (i2 != 0);

Write a program that declares an array of 10 pointers to char, then reads 10 lines from the user, dynamically allocates enough memory for each line after it is read, and makes successive members of the array point to the dynamically allocated memory. When done, the program must then release all the dynamically allocated memory.

#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *Array[10]; //declares an array of 10 pointers to char int i = 0; char line[100]; //char array to read and store a line int length; for(int i = 0; i < 10; i++){ fgets(line, 100, stdin); //read a line of max 100 characters length = strlen(line); //get the length of line string line[length] = '\0'; //place a Null character at the end of line Array[i] = malloc(sizeof(char) * length); //dynamically allocates enough money for each line strcpy(Array[i], line); //copy the line content to he dynamic memory } for(int i = 0; i < 10; i++){ //print the pointer index and the to which each pointer points printf("Pointer %d: Line: %strcpy", i+1, Array[i]); } for(int i = 0; i < 10; i++) //free the dynamic memory used for lines free(Array[i]); return 0; }

Write a program that takes integers as command line arguments, sums them, and returns the sum back to the operating system.

#include <stdio.h> #include <stdlib.h> //main or driver method that takes command line arguments int main(int argc, char *argv[]) { int sum = 0; //to store the sum //run a loop to iterate over all the command line arguments for(int i=1;i<argc;i++){ int num = atoi(argv[i]); // convert the argument to number sum = sum + num; //add it to sum } //return sum to OS return sum; }

Print out the errors in this program #include <stdio.h> #include <string.h> int main(int argv,char *argv[]) { char Buffer[128]; char *Data; Buffer = "the cat sat on the mat"; Data = &Buffer[5]; Buffer = Data; strcpy(Data,Buffer); return(0); }

*Data and Buffer need to be declared on different lines need to use strcpy to assign Buffer

What UNIX program is used to create libraries?

.o files can be combined into libraries using the ar utility.

What is the output from the following code segment? Index = 1; while (Index < 30) { if (Index % 2 == 0) { Index = Index < 10 ? ++Index : Index +3; continue; } Index = (Index << 1) + 1; if (Index % 6 == 0) { break; } printf("%d\n",Index); }

3 7 15 31 x<<y= x* 2^y x>>y=x/(2^y)

Explain the difference between a union and struct

A union is a variable which may hold (at different times) objects of different types and sizes A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenience. a union can only have one of its variables filled at a time whereas this limit is not on structs

Given the multidimensional array float values [4][3]; , what is the equivalent pointer expression for values [2][2]?

Answer: "("(values+2)+2)

What does the & operator return when applied to a variable?

Answer: The & operator returns the memory address of the variable.

At what point during a program's execution are {automatic|external|static} variables initialized?

Answer: Variable Initialization: Automatic: When the variable or object is invoked (variable defined) and disappears when the function has exited. External: Each function that uses an external variable/object must either declare it with an explicit extern declaration, or it must have been declared earlier in the same file to provide an implicit declaration. External (Global) variables may be defined outside any function and then exist independently of the functions. Static Static variables are local to the block in which they are declared so they must be defined in the same block/function if they want to be used. What makes static variables unique is that they remain permanent even after the function has exited.

Explain what realloc does, including the cases when the new size does not equal the old size.

Answer: realloc changes the size of a block of memory previously allocated with malloc or calloc whilst preserving the contents. Realloc returns a pointer to the new block or NULL if unsuccessful, in which case old is unchanged.

Which of the following are keywords in C?

Auto, double, int, struct, break,else,long,switch,case,enum,register,typedef,char,extern,return,union,continue,for, signed,void,do,if,static,while,default,goto,sizeof,volatile,const,float,short,unsigned

What are the four basic data types of C?

Char Int Float Double

What are the key features of {automatic|register|external|static} variables?

Key Features (These are Storage Classes): Automatic Variables/Objects: Local variables and formal parameters have, by default, the storage class auto. Automatic variables come into existance on invocation and disappear when the function is exited. Register Variables: A variable declared to be register informs the compiler that it will be used heavily. Where possible such variables are maintained in machine registers. This is achieved by preceding the declaration with the word register. Register can only be applied to automatic variables and function arguments (which are both automatic). Static Objects/Variables: Objects may be declared as static to make them permanent or private. This achieved by prefixing the definition with the word static. Internal static variables in functions remain in existence even after the function has exited and are very useful for this purpose. External Objects/Variables: All objects defined at the outer level in a file (the functions) are external objects. Externals are defined ONCE but used OFTEN. Each function that uses an external variable/object must either declare it with an explicit extern declaration, or it must have been declared earlier in the same file to provide an implicit declaration. External (Global) variables may be defined outside any function and then exist independently of the functions. Put externals at the end or in a separate file, and you'll force yourself to declare them.

Explain how a conditional expression may be rewritten using an if-then-else statement.

Let's say we have: z = a > b ? a : b; Now here z will have a if a>b else it will get b So it can be rewritten as: if(a>b) { z=a; } else { z=b; }

Explain the difference and similarity between these two variables: char *String1; char String2[128];

Similarity: Both array and pointer are passed by reference. Difference: *String1 is a dereferenced pointer while String2[128] is memory location which has a pointer pointing to its first value/location. The array makes space for 128 characters while pointer makes space for one character pointer

Which signal cannot be ignored or caught by a receiving process?

The SIGKILL signal

What is the difference between *i1 in the following two lines? int *i1; *i1 = 27;

The difference is that int *i1 simply declares an integer pointer i1 and *i1 =27 dereferences and defines that integer pointer with a value of 27.

What advantage do the execlp and execvp calls have over execl and execv?

They search the directories in the PATH environment variable - faster/efficent

Give an example showing how an array of pointers can be used to create a two dimensional array in which the rows are of differing lengths.

This is a multidimensional ragged array char * array[] = {"I", "like", "C", "programming"};

What is the exact output from the following code? Indicate spaces with an upsidedown triangle. int i1 = 27; float f1 = 13.13; float f2 = -45.45; char data[] = "Bonzai!"; printf("%3d %7.3f %-10s %f %o\n",i1,f1,data,f2,i1);

_27 __13.130 Bonzai!___ -45.45 33

What is an enumerated type?

a set of identifiers called enumeration constants. The syntax is the same as for structures and unions.enum [enumeration_tag] {<list of identifiers>} <variables>; Create your own variable type

Explain the difference between continue and break statements.

a. Continue- it continues to next iteration of the loop. b. Break- it breaks from the iteration and exits the loop

The kill system call sends a signal to a specified process. How can that process be prepared to receive the signal?

as specified in its uarea

Why should fgets be used in preference to gets?

both get a line from a file, but fgets gets a maximum of size-1 characters and appends \0 gets makes you responsible for making sure that the character array is big enough for the input - more errors

Explain how setjmp and longjmp can be used to retreat from complicated situations in one step.

can exit from deeply nested function calls setjmp stores the current state info longjmp returns to where setjmp saved

Rewrite the following while loop as a for loop. Index = 1; while (Index < MAX) { printf("%d\n",Index); Total += Index; Index++; }

for(Index=1; Index<Max; Index++;){ printf("%d\n", Index); Total += Index; }

Below is the content of a makefile. Assume that the files part1.c and part3.c have just been modified. Write down the sequence of command activations in the correct order when make is run. all: part1.o part2.o gcc part1.o part2.o -o whole part1.o: part1.c part3.o gcc -c part1.c gcc part1.o part3.o -o part3.out part2.o: part2.c gcc -c part2.c part3.o: part3.c gcc -c part3.c

gcc -c part1.c part3.c gcc -o whole part1.o part2.o part3.o

What system call can be used to get the IP address of a machine?

getaddrinfo()

What information is returned by the getpid, getppid, and getpgrp system calls?

getpid() - gets process ID of current program getppid() - gets process ID of the parent program getpgrp() - gets process group ID

What system call is designed for reading a password from a user?

getpwent();

What is the difference between the return values of the getuid and geteuid system calls?

getuid gets the real user ID whereas geteuid gets the effective user ID

What are the values of the variables after the following code is executed with input 56789 1234 45a72? int i1; float f1; char name[50]; scanf("%2d %f %*d %2s",&i1,&f1,name);

i1 = 56, f1 = 789.0, name = "45"

Assuming that the bits in an integer are numbered from 0, from right (least significant) to left (most significant), write an expression using bitwise logical operators to extract the 4 bits numbered 6 to 3 from an integer i1, and shift them all down so that bit numbered 3 ends up in position number 1.

i1>>2; << = left shift >> =right shift

Give an example showing how an array may be initialized when defined.

int myArray [2] = {1,2};

Name three system calls that cause (directly or indirectly) a signal to be sent to a process.

kill, sleep, wait

What is in a UNIX .a file?

library files

Given the structure definitions typedef struct { int Counter; float Average; } hit_rate_type; typdef struct { char *Name; hit_rate_type Killer; } murder_type; murder_type *JackTheRipper; write code that mallocs memory for JackTheRipper, stores the name "Jack the Ripper" in the Name field, and stores 99 in the Counter.

malloc(sizeof(murder_type)); *JackTheRipper.name = "jack the ripper"; *JackTheRipper.Killer.Counter = 99;

Explain the difference between memcpy and memmove

memcpy copies one buffer into another memmove is the same except the buffers may overlap

What user information stored in the /etc/passwd file?

name password UID (user id) GID (group id) personal information home directory shell program

What does the following program output? #include <stdio.h> #include <sys/types.h> #include <pwd.h> int main(int argc,char *argv[]) { struct passwd *P; while ((P = getpwent()) != NULL) { if (P->pw_uid == geteuid()) { printf("%s\n",P->pw_name); } } return(0); }

outputs the name of the user as stated in the /etc/passwd file

Which system call can be used to limit the amount of CPU time a process can use? Which system call can be used to find out home much CPU time the process did use?

setrlimit? clock_t returns the amount of CPU time used

What are the three predefined IO streams in UNIX, and what devices are they associated with by default?

stdin - initially associated with the keyboard stdout - initially associated with the screen stderr - initially associated with the screen

What errors might the following code produce? char string[128]; char *name; strcpy(string,name); strcpy(name,"old stuff");

strcpy(string,name); will cause a segmentation fault as name is not pointing to anything. It is not defined or initialized and as such it exists as a pointer pointing to essentially to junk (non-Null). This "junk" may or may not be owned by the user and as such causes a segmentation error at strcopy(string, name). strcopy(name, "old stuff") would cause a bus error as the name doesn't formally exist and as such if name doesn't exist how can "old stuff" be copied into nothing (non-Null). As such, it would cause a bus error.

In terms of make, what are targets, dependencies, and actions?

targets - programs to be built using a makefile dependencies - the objects, libraries and executables that depend on the source file actions - Actions are just standard UNIX commands that are fed to a shell

What is the output from the following code segment? int i1 = 27; int i2 = 29; if (i1++ >= --i2) { i1++; printf("%d %d\n",i1++,i2--);} else { i2++; printf("%d %d\n",--i1,++i2); } printf("%d %d\n",i1,i2);

the if statement makes em 28 and 28 Goes into the else somehow output: 27 30 27 30

typedef a structure suitable for holding information about a radio station, including it's 4 character code name (e.g., ZETA), it's catch phrase (e.g., "ZETA Rocks"), it's frequency (e.g., 94.9), and the number of employees (e.g., 27).

typedef struct { char code[4]; char *phrase[]; float frequency; int employees; } radioStation;


संबंधित स्टडी सेट्स

Part C rights and responsibilities

View Set

Chapter 4: Life Insurance Policy Provisions, Options and Riders

View Set

Chapter 3- Stress--its Meaning, Impact and Sources

View Set

What is a Machine? Section 8.2 quiz

View Set

Chapter 4: Listening Effectively

View Set

Oklahoma Insurance Adjuster's License

View Set