I have an online test for an introductory computer science course, which just opened and is available for a day. The material is on Unix scripting, as well as basic C stuff. Attached are some practice questions for the test, as well as a cheat sheet that was given to us (everything on there is fair game, otherwise stick with basic syntax). Time limit is 2.5 hours, ~20 questions mostly multiple choice and some short answer. Will provide login to online portal.
Question 1 Bash Programming QUESTION: Using Advanced Unix commands Write a Bash shell script delta.sh that accepts two directories as input and then displays the names and number of lines in the file for all the files that have the file extension .txt and have the identical name in both directories. Your shell script should display an appropriate error message and terminate if it is not passed two arguments or if either of the arguments passed is not the name of a valid directory or if the two arguments are identical. The script should also ignore any sub directories (as well as files under them) that are inside the original directories passed as its argument. $ ./delta.sh dir1 dir2 me.txt 100 150 Notes.txt 10 75 $ In the above example the files me.txt and Notes.txt existed in both directories, but me.txt in dir1 was 100 lines (a line is a series of characters terminated by a carriage return) and the file me.txt in dir2 has 150 lines. Question 2 C Programming QUESTION: (A) Write a C Program, palindrome.c , that checks if a word passed as its argument is a palindrome. A palindrome is a word that spells the same from left to right and right to left (e.g. mum, madam, level ). The program should terminate with exit code 0 for a palindrome and 1 for a non palindrome word. To simplify things, you can assume that the argument consists of a word consisting of only lowercase letters. However, your program logic should work for a word of any length. Your program must not use any arrays (other than the one used to capture the arguments passed to the main). Below is an example of its invocation. $ ./palindrome level $ echo $? 0 $ Question 3: Write the following C program in the space provided [20 points] Create a STUDENT struct with two fields: char name[100] and float GPA. Create an array of 100 students. Populate the entire array with data input from the keyboard. If the user input a student with a GPA lower than 3, do not store that in the array. The program ends by printing the average GPA and the name of the first student with the highest GPA to the screen. Assume student and gpa are valid. Question 4: Provide a short answer for each of the following terms. Your answer must fit within the space provided in the table. QUESTION ANSWER What does the system() command do? What does exit(1) do? What does x receive? x=(5<10)?5:10; define="" call-by-reference="" define="" call-by-value="" in="" pointers,="" what="" does="" the="" asterix="" (*)="" and="" the="" ampersand="" (&)="" do?="" find="" cheat="" sheet="" find="" cheat="" sheet="" $="" find="" ./="" -name="" '*e.txt'="" ./one.txt="" ./three.txt="" ./five.txt="" $="" find="" ./="" -name="" '*e.txt'="" -exec="" ls="" -l="" '{}'="" \;="" -rw-------="" 1="" jdsilv2="" root="" 0="" jul="" 27="" 19:00="" ./one.txt="" -rw-------="" 1="" jdsilv2="" root="" 0="" jul="" 26="" 19:00="" ./three.txt="" -rw-------="" 1="" jdsilv2="" root="" 0="" jul="" 25="" 19:00="" ./five.txt="" ls="" is="" executed="" three="" times="" as="" follows:="" ls="" -l="" ./one.txt="" ls="" -l="" ./three.txt="" ls="" -l="" ./five.txt="" command="" to="" execute.="" options="" to="" command="" (if="" any,="" can="" have="" many).="" output="" from="" ”find”="" is="" passed="" to="" the="" command="" here.="" tells="" find="" that="" there="" is="" ”no="" more”="" arguments="" to="" pass="" to="" the="" command.="" quotes="" and="" escape="" character="" is="" important="" so="" that="" the="" shell="" does="" not="" “eat”="" the="" brackets="" or="" the="" semicolon.="" $="" find="" ./="" -name="" '*e.txt'="" -exec="" ls="" -l="" '{}'="" +="" -rw-------="" 1="" jdsilv2="" root="" 0="" jul="" 27="" 19:00="" ./one.txt="" -rw-------="" 1="" jdsilv2="" root="" 0="" jul="" 26="" 19:00="" ./three.txt="" -rw-------="" 1="" jdsilv2="" root="" 0="" jul="" 25="" 19:00="" ./five.txt="" ls="" is="" executed="" a="" single="" time="" as="" follows:="" ls="" -l="" ./one.txt="" ./three.txt="" ./five.txt="" sed="" cheat="" sheet="" change="" all="" occurrence="" of="" sort="" to="" sort="" and="" mysort="" to="" mysort.="" $="" sed="" -e="" 's/sort/sort/g'="" -e="" 's/mysort/mysort/g'="">10)?5:10;>< program.log="" msg:="" program="" mysort="" starting="" ...="" msg:="" data="" load="" time="" 24="" seconds.="" warn:="" no="" sort="" algorithm="" specified,="" defaulting="" to="" bubble="" sort.="" msg:="" starting="" sort.="" msg:="" time="" to="" sort="" is="" 43="" seconds.="" msg:="" number="" of="" records="" sorted:="" 45310030="" msg:="" mysort="" exit="" code="" is="" 0="" msg:="" program="" mysort="" terminating.="" $="" why="" is="" mysort="" not="" replaced="" by="" mysort="" $="" sed="" -e="" 's/sort/sort/g'="" -e="" 's/mysort/mysort/g'="">< program.log="" applied="" first="" applied="" second="" -="" but="" there="" is="" no="" mysort="" to="" find.="" it="" has="" become="" mysort.="" awk="" cheat="" sheet="" include="" the="" number="" of="" records="" processed="" in="" the="" output.="" $="" awk="" '="" begin="" {ltime="0;stime=0}" load="" time/="" {="" ltime="$5" }="" time="" to="" sort/="" {="" stime="$6" }="" records="" sorted/="" {="" records="$NF" }="" end="" {print="" records,="" ltime+stime}="" '="">< program.log="" 45310030="" 67="" $="" nf="" represents="" the="" total="" number="" of="" fields="" in="" a="" line.="" $nf="" -=""> Value of the last field in a line. Similarly, NR represents the “current” line number. What does this program do ? $ awk '{ print "Line ", NR, ":", NF, " fields -: ", $0 }' < program.log="" $="" awk="" '{="" if($0="" ~="" sort/)="" {="" print="" $0="" }="" }'="">< program.log="" ~="" is="" for="" pattern="" matching.="" patterns="" can="" be="" regular="" expressions.="" $="" awk="" '="" begin="" {fs="," }="" {="" if="" (nf="" !="2)" {="" print="" nr,"="" has="" ",="" nf,="" "="" fields"="" }="" }="" '="">< uncleandata.csv="" sort="" cheat="" sheet="" $="" cat="" students.txt="" marc="" pane="" alex="" lapine="" micheal="" clark="" jessica="" quaid="" ting="" lee="" juan="" dare="" jeremy="" bill="" jessica="" land="" yannick="" bath="" nicolas="" cage="" john="" pane="" jean-sebastien="" pierre="" nadeem="" ali="" $="" $="" sort="" -k2,2="" students.txt="" nadeem="" ali="" yannick="" bath="" jeremy="" bill="" nicolas="" cage="" micheal="" clark="" juan="" dare="" jessica="" land="" alex="" lapine="" ting="" lee="" john="" pane="" marc="" pane="" jean-sebastien="" pierre="" jessica="" quaid="" $="" sort="" based="" on="" this="" range="" of="" fields="" as="" the="" key.="" at="" +="" crontab="" cheat="" sheet="" $="" at="" now="" +="" 25="" minutes="" execute="" 25="" minutes="" from="" now.="" other="" keywords="" -="" hours="" days="" months="" years="" $="" at="" 3:04pm="" specific="" time.="" $="" at="" 3:04pm="" friday="" $="" at="" 1:00am="" tomorrow="" $="" at="" 4:00pm="" +="" 3="" days="" $="" at="" 2:00pm="" august="" 17="" 2020="" code="" cheat="" sheet="" program="" process="" 7="" built-in="" c="" language="" types="" description="" reserved="" word="" bits="" range="" integer="" short="" 8="" -="" 128="" to="" +="" 127="" int="" 16="" +/-="" 32,768="" long="" 32="" +/-="" 2,147,483,648="" floating="" point="" float="" 32="" +/-="" 3.4="" x="" 1038="" with="" 7="" significant="" digits="" double="" 64="" +/-="" 1.7="" x="" 10308="" with="" 15="" significant="" digits="" boolean="" short,="" int,="" long="" (0="" is="" false,="" other="" true)="" character="" char,="" unsigned="" short="" int="" 8="" 0="" to="" 255="" string="" char="" *="" 32="" address="" in="" memory="" (special="" case="" of="" pointer)="" pointers="" type*="" 32="" address="" in="" memory="" actual="" sizes="" depend="" on="" cpu="" types="" cheat="" sheet="" 8="" #include="">
Cheat Sheet • Standard math: • double y = sqrt(double); • double y = pow(base,exponent); • int x = abs(int); • double y = fabs(double); • double x = floor(double); • double x = ceil(double); • Trigonometry: • sin, cos, tan, asin, acos, atan X = sqrt(25); X = pow(10,2); 10.9 -> 10.0 10.2 -> 11 Escape characters Cheat Sheet • Escape characters are used for formatting: • The backslash (\) character • \n new line • \t tab • \a bell • \b backspace with no erase • \r carriage return • \\ backslash • The percentage (%) character formats variables • Format: % SIGN SIZE TYPE • %: required • SIGN: + -, optional • + = normal justification, - = reverse justification • SIZE: integer, optional • TYPE: d,c,f,s, required • d = integer, c = character, f = float, s = string 10 #include Cheat Sheet • NULL = 0 • EXIT_FAILURE = 1 • EXIT_SUCCESS = 0 • int x = rand(void); // 0 to RAND_MAX • int system(string) • float x = atof(string) • int y = atoi(string) • int z = abs(int) • void exit(int) 32767 11 #include Cheat Sheet • Case manipulation: • int c = toupper(int); • int c = tolower(int); • Character testing: • int x = isalpha(int); • int x = isalphanum(int); • int x = isdigit(int); •Note: char is in int. if(toupper(c)=='X') if(isalpha(c)) Arrays and pointers Cheat Sheet • char *s = “Bob”; • char a[] = {'B', 'o', 'b', '\0'}; • char c[4]; • scanf("%s", c); What do these look like physically in memory? 'B' 'o' 'b' '\0' 'B' 'o' 'b' '\0' s a c Variable. They can be assigned to another structure of similar shape!! Variable. Each cell of the array is a variable and can be assigned a different value. Static. It is a string. It will be the ASCII values of these characters. Pointers Cheat Sheet • By example: • int x = 5; // is a simple structure that stores integer numbers • int *p; // create a variable that can store the address of another structure • p = &x; // p has been assigned the address of x p is said to be “pointing to” x p does not have the value of x, p just “knows” where x is located in the computer’s memory • printf("%d", x); // prints 5 to the screen • printf("%d", *p); // prints 5 to the screen • printf("%d", p); or %p // ???? printf("%d", &x); printf("%s",&p); 13 & → return the address of a structure (referencing) * → using the address, get the value from the location in the computer’s memory (derefrencing) 14 #include Cheat Sheet •int strcmp(char *s1, char*s2) •int strncmp(char *s1, char*s2, int len) •int strlen(char *string) •char *strcpy(char *dest, char*src) … strncpy •char *strcat(char *dest, char*src) … strncat •void *memset(char* string, char character, int len)