Q-A
3
1. Given the following array: char values[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' }; How many comparisons will be made using the binary search to find the value 'B'?
True
A pointer provides an indirect means of accessing the value of a particular data item.
algorithm
A sequence of instructions that solves a problem is called a(n) _____ .
symbolic link
A type of file that points to another file is called a ______.
argument, parameter
A(n) _________ is information that is passed to a function, and a(n) _________ is information that is received by a function.
Quick sort
Among the sorting algorithms presented, which is the most efficient: quick sort or selection sort?
the same data type
An array can store a group of values, but the values must be _____.
True
An individual array element can be processed like any other type of C variable.
initialized, declared
Arrays may be _____ at the time they are _____.
-3
Assume that x is an int variable. What value is assigned to x after the following assignment statement is executed? x = -3 + 4 % 6 / 5;
Pre-test
The while loop is this type of loop.
logical
These operators connect two or more relational expressions into one or reverse the logic of an expression.
==
This operator is used in C++ to represent equality.
Local
This type of variable is defined inside a function and is not accessible outside the function.
a loop to assign the elements of one array to the other array
To assign the contents of one array to another, you must use _____.
True
To set a pointer named testPtr to point to the first element in the tests array, you would code it like this: testPtr = &tests[0];
void *existing_memory, size_t new_size
To use the realloc memory allocation function, what parameters do you need to include with the function call?
The first error only
Given a list of syntax errors from a compiler, a programmer should focus attention on which error(s), before recompiling?
4
Given the following array: double rainfall[] = { 2.2, 4.4, 6.6, 1.1, 3.3, 5.5 }; How many exchanges will be made using the selection sort?
False
Given the following array: float grades[ ] = { 3.3, 1.1, 7.7, 4.4, 9.9, 2.2 }; Will the binary search find the value 2.2?
6
Given the following array: int scores[ ]={83, 62, 39, 101, 82, 77, 97}; How many comparisons will be made using the linear search to find the value 77?
15
Given the following recursive function: int sum(int n) { if (n <= 1) return n; else return (n + sum(n - 1)); } What value will be returned if the value 5 is passed into the function?
exec
Under normal conditions, signals are set to their default action, unless the process that calls the ______ is ignoring the signal.
HI Hello $HI \$HI Displays nothing HelloJ
Given the following script file, match the command with the output: #!/bin/sh # HI=Hello echo HI echo $HI echo \$HI echo '\$HI' echo "$HIJ" echo "${HI}J"
(yes | y | Yes | YES)
How would you test if $timeofday is yes, y, Yes, or YES in the following code snippet? case "$timeOfDay" in
Null statement
If you place a semicolon after the test expression in a while loop, it is assumed to be a(n):
Initialization
In a for statement, this expression is executed only once.
All of these: data type(s) of the parameter data type of the return value name of the function
In a function header, you must furnish the ______.
3 * 2
In the following C statement, what will be executed first according to the order of precedence? result = 6 - 3 * 2 + 7 - 10 / 2 ;
g*.sh
In the following script, you want to print all the script files that end with .sh and begin with the letter g. What wildcard phrase would you add in the tag below? #!/bin/sh for file in $(ls <insert wildcard code here>); do lpr $file done exit 0
Memory
In what component does a processor store the processor's required instructions and data?
<
Look at the following statement: while (x++ < 10) Which operator is used first?
SIGALRM - Generated by an expired timer SIGHUP - Disconnecting terminal SIGINT - Terminal interruption SIGKILL - Forcibly terminate a process SIGPIPE - Writing to a pipe with no readers SIGTERM - Default signal sent from the kill command SIGFPE - Floating point arithmetic exception SIGILL - Illegal instruction executed SIGQUIT - Configured quit character SIGSEGV - Exceeding array boundaries or dereferencing an invalid pointer.
Match each term with the correct definition.
True
Users can change the LOGNAME environment variable.
The program will continue to run.
Using the following code snippet: int main() { signal(SIGINT, SIG_IGN); printf("Outside "); while(1) { printf("Inside "); sleep(1); } } What will happen when the user presses CTRL-C to terminate the program after the loop iterates 5 times?
food meal[10];
Suppose that the following structure is used to write a dieting program: struct food { char name[15]; int weight; int calories; }; How would you declare an array meal[10] of this data type?
Yes Yes No!
Using the following code snippet: int main() {int j; double *t, num[5]; signal(SIGSEGV, func1); num[0] = 0.0; t = 0; for (int k = 1; k < 5; k++) { num[k] = (double)k + 11.0; if (num[k-1] > 12.0) *t = 55.5; printf("Yes "); } } void func1(int signum) { printf("No! "); exit(SIGSEGV); } What is the output? (Hint: a SIGSEGV is generated on an illegal storage access)
4
Using the following code snippet: int main() {signal(SIGFPE, func); for (int j = -3; j <= 3; j++) printf("%d ", 12/j); } void func(int signum) {printf("You cannot divide by zero!"); exit(SIGFPE); } On which iteration of the for loop will the SIGFPE be generated?
The parent process uses the kill() function to send the SIGUSR1 signal to the child process and then send the prompt "Command?".
Using the following code snippet: int running; int main() { int j; char text[80]; j = fork(); if (j == 0) { signal(SIGUSR1, f); running = 1; while(running == 1) sleep(1); } else { while(1) { sleep(1); printf("Command? "); scanf("%s", text); if(strcmp(text, "go") == 0) kill(j, SIGUSR1); if(strcmp(text, "stop") == 0) break; } } } void f(int signum) { printf("Go received\n"); running = 0; } What happens when the user enters "go" at the "Command?" prompt?
strcpy(d1.name, "Mercury");
Using the following structure: struct data { char name[15]; double distance; double weight; }; data d1; How would you set the name for d1 to the value "Mercury"?
It is testing if the input file "status" failed to open.
What condition is the following line of code testing? if ((fp = fopen("status", "r")) == NULL)
Answer: Adds up the size of all files in a directory.
What does the following code do? #!/bin/sh # value = 'ls - l | cut - b 30 - 32' # adjust the cut columns as necessary total = 0 for i in $value do total = 'expr "$total" + "$i"' echo $i done echo $total
Space is allocated for pointer p and check if the malloc call is successful.
What does the following code segment do? char *p; int size = 1048576; p = (char *)malloc(size); if (p != NULL) {
Reads in a value from the input file and writes it to the output file.
What does the following code snippet do? int sum = 0, val;FILE *ifp, *ofp; ifp = fopen("infile", "r");ofp = fopen("outfile", "w"); while (fscanf(ifp, "%d", &val) == 1)sum += val;fprintf(ofp, "%d\n", sum);
Reads the next line of characters from the file pointed to by ifp.
What does the following function call do for an input file stream pointer ifp? fgets(line, MAXLINE, ifp);
Renames all files to lowercase letters
What does the following script do? #!/bin/sh # files = 'ls' for name in $files do newname = 'echo $name | tr "[:upper:]" "[:lower:]" ' if["$newname" != "$name"]; then mv "$name" "$newname" fi done
for i in 1 2 5
What for loop would you add to get the following output? file1.txt file2.txt file5.txt Script file: #!/bin/sh # <insert for loop here> do echo "file${i}.txt" done
The program will display "Error: Do it again. "on the console screen.
What happens if the user enters a -44 when prompted: #define MAXLINE 500 int main (void) { char line[MAXLINE]; int what, n; do { printf("Input a positive integer: "); fgets(line, MAXLINE, stdin); what = sscanf(line, "%d", &n) != 1 || n <= 0; if (what) printf("\nError: Do it again.\n"); } while (what); }
A type of file used for communication between processes.
What is a FIFO?
A type of file used for network communication between processes.
What is a socket?
5 ! 4 ! 3 ! 2 ! 1 ! Blast Off!
What is displayed when the following code is executed? #include <stdio.h> void count_down(int n); int main() { count_down(5); } void count_down(int n) { if (n) { printf("%d ! ", n); count_down(n - 1); } else printf("Blast Off!\n"); }
A, B, A
What is the output of the following code snippet: char c1 = 'A', c2 = 'B', tmp; char* p = &c1, * q = &c2; tmp = *p; *p = *q; *q = tmp; printf("%c, %c, %c", tmp, *p, *q);
b = 11
What is the output of the following code snippet: int a = 3; int b = 7; int* p = &b; *p = 2 * *p - a; printf("b = %d", b);
2, 7, 3
What is the output of the following code snippet? int a = 2; printf("%d ", a); { int a = 7; printf("%d ", a); } printf("%d ", ++a);
First name: John Last name: Brown Age: 27
What is the output of the following code snippet? int main(void) { char myString[100] = "John Brown 27"; // Input string char firstName[50]; // First name char lastName[50]; // Last name int userAge; // Age // Parse input, break up into first/last name and age sscanf(myString, "%49s %49s %d", firstName, lastName, &userAge); // Output parsed values printf("First name: %s\n", firstName); printf("Last name: %s\n", lastName); printf("Age: %d\n", userAge); return 0; }
Titan
What is the output of the following code? #!/bin/sh # NAME="Godzilla" MONSTER="" if [ "$NAME" != "Godzilla" ]; then echo Goodbye Tokyo! fi if [ -z "$MONSTER" ]; then echo Titan! fi if [ -n "$MONSTER" ]; then echo Monarch? fi
Matches
What is the output of the following code? #!/bin/sh # NAME="Jupiter" i=7 j=9 if [ $NAME = "Jupiter" ]; then echo Matches fi if [ $i -ge $j ]; then echo Lesser fi
10 2
What is the output of the following code? #!/bin/sh # i=3 j=7 a = 'expr $i + $j' b = 'expr $j / $i' echo "$a $b"
Blast Off! 1 ! 2 ! 3 ! 4 ! 5 !
What is the output of the following code? #include <stdio.h> void count_down(int n) { if (n) { count_down(n - 1); printf("%d ! ", n); } else printf("Blast Off!"); } int main() { count_down(5); }
12 4 2 3
What is the output of the following program? #include <stdio.h> int a = 1, b = 2, c = 3; int f(void); int main() { printf(" % 3d ", f()); printf(" % 3d % 3d % 3d ", a, b, c); } int f(void) { int b, c; a = b = c = 4; return (a + b + c); }
There is an error in the script.
What is the output of the following script if the user types in 2.50 at the prompt? #!/bin/sh # ROCKS=4 echo Price? read #$PRICE echo $ROCKS rocks for sale, $PRICE each
12
What is the value of donuts after the following code executes? int donuts = 10; if (donuts != 10) donuts = 0; else donuts += 2;
Contains the names of other files and pointers to information on the files.
What kind of information does a directory file contain?
SIGQUIT
What signal is generated when the user presses the CTRL-\ key when a program is executing?
CTRL-Z
What suspend character generates a SIGTSTP signal?
11
What value is returned from the function if the value 5 is passed to the parameter n? int sum(int n) { if (n <= 1) return n; else return (n + sum(--n)); }
55
What value is stored in element 3? int numbers[ ] = { 99, 87, 66, 55, 101 };
Generate an error.
What will a compiler do for the following code? /* numItems = 2; /* Total items to buy */ rate = 0.5; */
tmp1 contains "aaa" and tmp2 contains "abc"
What will be stored in the output files tmp1 and tmp2 when the following code executes? #include <stdio.h> main() { char c, s[] = "abc", * p = s; int i; FILE* ofp1, * ofp2; ofp1 = fopen("tmp1", "w"); ofp2 = fopen("tmp2", "w"); for (i = 0; i < 3; ++i) { sscanf(s, "%c", &c); fprintf(ofp1,"%c", c); } for (i = 0; i < 3; ++i) { sscanf(p++, "%c", &c); fprintf(ofp2, "%c", c); } }
Hello!
What will be the output when the following code is executed: int main() { printf("Hello! "); fclose(stdout); printf("Goodbye!"); }
20
What will be the value of result after the following code has been executed? int a = 60; int b = 15; int result = 10; if (a = b) result *= 2;
Enable core dumps when debugging
What will the following code segment do? #ifdef DEBUG struct rlimit limit; limit.rlim_cur = RLIM_INFINITY;limit.rlim_max = RLIM_INFINITY; if (setrlimit(RLIMIT_CORE, &limit) < 0) err_ret("can't change the coredump limit"); #endif
dlrow olleh
What will the following program display if the phrase "hello world" is entered at the prompt? #include <stdio.h> void print(void); int main() { printf("Input a line: "); print(); } void print(void) { char c; if ((c = getchar()) != '\n') print(); putchar(c); }
13
What will the value of x be after the following statements execute? int x = 0; int y = 5; int z = 4; x = y + z * 2;
Reference
When used as parameters, these types of variables allow a function to access the parameter's original argument.
7
Which line in the following program will cause a compiler error? 1 #include <stdio.h> 2 3 4 int main(void) 5 { 6 const int MY_VAL; 7 MY_VAL = 77; 8 printf ("MY_VAL = %d", MY_VAL); 9 return 0; 10 }
calloc
Which memory allocation function would you use to allocate dynamic memory for an array of structures?
Opening a file in append mode that does not exist in the stated directory.
Which of the following conditions would NOT cause a file failure?
x<=y
Which of the following expressions will determine whether x is less than or equal to y?
SIGTSTP
Which of the following is considered to be a job control interactive stop signal?
"ra"
Which of the following is not a valid mode to open a file?
Functions that use global variables.
Which of the following would not make a function reentrant?
An infinite limit is specified by the constant RLIM_NOLIMIT.
Which one of the following is not one of the rules that govern the changing of resource limits:
O_WRONLY and O_RDWR
Which permission flags determine whether an existing file may be open for writing?
O_RDONLY and O_RDWR
Which permission flags determine whether an existing file may be opened for reading?
number = number + 1;
Which statement is equivalent to the following? number += 1;
st_mode
Which value encodes the access permission bits for a file?
This provides a way to terminate the process when it tries to dereference a null pointer.
Why is the location 0 in the data segment not accessible when a program is executed?
False
Will the following declarations compile correctly? struct circle1 { double radius; struct circle2 area; }; struct circle2 { double radius; struct circle1 area; };
True
Would the following code snippet compile if testPtr is an integer pointer properly initialized to a value and the contents of the test array are integers? if (testPtr > &test[5])
False
You may create a new file in a directory as long as you have write permission.
Post-test
The do-while loop is considered a(n) _________ loop.
what file permissions are set for the user
The effective user ID and effective group ID determine _________.
True
The feof() function returns if the previous read operation reached the end of the file.
the password file when the user logs into the system
The real user ID and real group ID are taken from ___________.
call
Functions are ideal for use in menu-driven programs. When a user selects a menu item, the program can ________ the appropriate function.
Calling the kill function. Executing a return from a user-defined function.
Choose all that apply: A process can terminate normally in any of these ways except:
printf("I wish\n"); printf("you were here");
Choose the statement(s) that generate(s) this output: I wish you were here
Unprivileged processes can't change their group IDs.
Consider a user who may have multiple roles in an organization and may belong to several supplementary groups. What can be used to change the user's effective group ID to one of its supplementary group IDs when creating new files for different workgroups?
False
Copies of argc and argv are kept as global variables like environ variables are.
