CPS 444/544: Practice Problems

Note: For any regular expressions given below (shell, filter, or otherwise), box and rightarrow indicate one space and one tab, respectively.

(added September 8, 2005)

C programming

  1. What problem may occur with the following code?
    char c;
    
    while ((c = getchar()) != EOF) {
       ...
    }
    
  2. Is one of the following assignments incorrect in C? Explain.
    struct node *p, *q;
    
    p = malloc (3*sizeof (struct node));
    q = (struct node *) malloc (3*sizeof (struct node))
    
  3. A program once contained the following:
    #include <math.h>
    ...
    y = cos (x);
    ...
    
    and yet the definition of the cosine function was not found. What happened?

  4. What's wrong with the following recovery?
    printf ("Enter your age:\n");
    while (scanf ("%d", &age) < 1)
        printf ("Error.  Try again:\n");
    
  5. Essentials, File manipulation, and Korn Shell

  6. To log off of the Korn shell, you should
    1. enter EOF
    2. enter stop
    3. enter logoff
    4. enter logout
    5. enter bye

  7. To list your . files, enter
    1. dot
    2. .
    3. ls -l
    4. ls -F
    5. ls -a

  8. To list all the files ending in .c or .f, enter
    1. ls *[cf]
    2. ls *.[c|f]
    3. ls *.[cf]

  9. Which of the following are not shell metacharacters (give all that apply)?
    1. $
    2. .
    3. &
    4. |
    5. /
    6. \

  10. Give and explain the output of the following command lines:
    1. $ touch /
    2. $ touch \/
    3. $ touch '/'

  11. Explain the difference between a program and a process.

  12. Which command indicates which computer you are logged into?

  13. Which command indicates the name and version of the OS running on the computer into which you are logged?

  14. Explain why it is be a good idea to single quote the character to which you set a kernel metacharacter using stty (e.g., $ stty kill 'a').

  15. When does the kernel start interpreting its metacharacters?

  16. When does the shell start interpreting its metacharacters?

  17. (courtesy exercises 1-1 and 1-2, p. 7 of [KP]) Start with the following environment:
    $ stty kill '@'
    $ stty erase '#'
    $ stty lnext '\'
    $ sh
    
    Explain the results of each of the commands in the following transcript:
    $ date\@
    date@: not found
    $ date
    Fri Sep  2 09:10:45 EDT 2005
    $ #date
    Fri Sep  2 09:10:45 EDT 2005
    $ \#date
    

  18. Assume the pound # and backslash \ characters serve as the erase and escape kernel metacharacters, respectively, and that, for each filename pattern-matching expression, there is at least one file which matches that expression. Explain the output of each of the following Korn shell command lines (assume each is entered at the keyboard; something else might appear on the screen).
    1. $ \# pwd
    2. $ ls myfile > thisfile
    3. $ ps
    4. $ ls -ld .

  19. To append output from an executable file pgm to a file data, enter:
    1. pgm | data
    2. pgm > data
    3. pgm >> data

  20. In vi, to delete three words forward from the cursor, enter
    1. d3w
    2. 3dd
    3. 3x
    4. d3f

  21. To read a file trig.c into vi at the cursor position, enter: (assume trig.c resides in the directory from which vi was started and that you are in command mode)
    1. r trig.c
    2. :r trig.c
    3. <esc>r trig.c

  22. What is a daemon process? Give an example.

  23. Give 5 hallmarks of Unix.

  24. What is the difference the between the filename value of the ENV variable and the .profile file. When is each sourced?

  25. Explain the sequence of steps that the system follows from the time one enters their password successfully up until the first prompt for input is displayed.

  26. Consider the following series of commands and outputs:
    $ cat .profile
    ENV=".myenv"; export ENV
    ADDENV=".kshrc"
    export PS1="Go ahead $ "
    EXINIT="showmode showmatch ruler"
    . $ADDENV
    $ cat .kshrc
    . $HOME/.profile
    $ cat .myenv
    
    Identify the most critical problem above?

  27. Consider the following series of commands and outputs:
    1 $ cat .profile
    ENV=".myenv"; export ENV
    ADDENV=".kshrc"
    export PAGER=less
    EXINIT="showmode showmatch ruler"
    . $HOME/.kshrc
    2 $ cat .kshrc
    alias ll=ls -l
    3 $ cat .myenv
    . $HOME/.kshrc
    $ ksh
    4 $ echo $PAGER 
    5
    6 $ echo $EXINIT
    7
    
    1. What is printed on line 5?
    2. What is printed on line 7?

  28. Consider the following series of commands and outputs:
    1 $ cat .profile
    ENV=".kshrc"; export ENV
    ADDENV=".myenv"
    export PAGER=less
    EXINIT="showmode showmatch ruler"
    . $HOME/$ADDENV
    2 $ cat .kshrc
    alias ls=dir
    alias cd..=cd ..
    alias goup=cd ..
    3 $ cat .myenv
    alias ll=ls -l
    $ ksh
    4 $ echo $PAGER 
    5
    6 $ echo $EXINIT
    7
    
    1. Identify a potential problem above.
    2. What is printed on line 5?
    3. What is printed on line 7?

  29. Consider the following series of commands:
     1 $ cat S 
     2 A=hello
     3 export B=world
     4 export ENV=""
     5 $ . S
     6 $ ksh & S
     7 $ ksh
     8 $ echo $A ()
     9
    10 $ export A=bye
    11 $ echo $B (world)
    12
    13 $ B=planet
    14 $ echo $A (bye)
    15
    16 $ echo $B (planet)
    17
    18 $ ksh
    19 $ echo $A (bye)
    20
    21 $ echo $B (planet)
    22
    23 $ exit
    24 $ echo $A (bye)
    25
    26 $ echo $B (planet)
    27
    28 $ exit
    29 $ echo $A (hello)
    30
    
    
    1. What is printed on line 9?
    2. What is printed on line 12?
    3. What is printed on line 15?
    4. What is printed on line 17?
    5. What is printed on line 20?
    6. What is printed on line 22?
    7. What is printed on line 25?
    8. What is printed on line 27?
    9. What is printed on line 30?

  30. Assume your login shell is Korn.
    1. What does the PATH variable do?
    2. How would you change the value of the PATH variable by extending it with $HOME/bin at the beginning of its current value, in such a way that this new value would be in effect each time you logged in, and its value would also affect all descendant processes of your login shell?
    3. Assume you have an executable file $HOME/bin/ls and your PATH is modified as indicated above. Explain what the $ which ls command line would output and why.

  31. The Korn shell. Explain the symbols and the effect (output) of the command (assume each of the following appears as shown on the screen). For full credit explain which entity (shell or command) is interpreting which metacharacter. Draw diagrams if necessary.
    1. $ echo "## But 10 < 12 ##"
    2. $ export PS1="What now? "
    3. $ print "Take the correct $PATH"
    4. $ echo \$HOME
    5. $ mv *.c *.cpp
    6. $ type which
    7. $ whereis whatis
    8. $ whereis which
    9. $ which whereis

  32. Find an example where ksh and csh differ in their behavior. Click here for an example (do not use any of these points of departure).

  33. List and describe succinctly one item from each of the first three sections of the Unix Reference manual. Each of these items must be accessible using the man command on our system. Do not copy whole pages from the manual. Instead, phrase the explanations in your own words. Click here for an example (do not use any of these entries).

  34. Do not give the definitions, but for each of the following, state in which section (1, 2, or 3) of the Unix Manual you would find it described, with brief reasons.
    1. strlen
    2. bash
    3. read

  35. Explain the difference between an absolute and a relative path.

  36. Give a directory owned by root in which you have write permissions.

  37. Give examples of three top-level subdirectories (i.e., children of root /) and a brief description of the role of each.

  38. (courtesy exercise 1-4, p. 29 of [KP]) Consider the file junk.
    
    
    Take one sentence to explain the output of each of the
    following command lines (there are 10):
    
    

    $ ls junk
    $ echo junk
    $ ls /
    $ echo /
    $ ls
    $ echo
    $ ls *
    $ echo *
    $ ls '*'
    $ echo '*'

    For each of the of the rows above compare the command line in the first column to that in the second column.

  39. (courtesy exercise 1-5, p. 31 of [KP]) Explain why
    $ ls >ls.out
    
    causes ls.out to be included in the list of files.

  40. (courtesy exercise 1-6, p. 31 of [KP]) Explain the output from
    wc temp > temp
    
    If you misspell a command name, as in
    $ woh >temp
    
    what happens?

  41. Create a file named -r. Show how you did this. Now remove the file with the rm command. Show how you did this.

  42. (courtesy exercise 2-8, pp. 63 of [KP]) cp doesn't copy subdirectories, it just copies files at the first level of a hierarchy. What does it do if one of the argument files is a directory? Is this kind or even sensible? Discuss the relative merits of three possibilities: i) an option to cp to descen directories, ii) a separate command rcp (recursive copy) to do the job, or iii) just having cp copy a directory recursively when it finds one. What other programs would benefit from the ability to traverse the directory tree?

  43. (added September 13, 2005)
  44. Consider the following session with the Korn shell:
    $ pwd
    $ /home/cps444-n1.19
    $ ls
    C/ bin/ text/ wc.c
    $ cd C
    $ pwd
    $ /home/cps444-n1.19/C
    $ ls
    cat.c myshell.c mine.c
    $ cd text
    $ pwd
    $ /home/cps444-n1.19/text
    
    Give and explain two directories in the user's CDPATH.

  45. Is export a shell built-in or a Unix command? Show how to determine the answer?

  46. Consider the following to account setups:

    Setup 1:
    $ cat .profile
    ...
    export ENV=$HOME/.kshrc
    alias ll="ls -l"
    ...
    $ cat .kshrc
    . $HOME/.profile
    
    Setup 2:
    $ cat .profile
    ...
    export ENV=$HOME/.kshrc
    ...
    $ cat .kshrc
    alias ll="ls -l"
    
    Which setup is advisable and why?

    (added October 6, 2005)

    More C programming

  47. Write a C program which reads two integers from stdin, a base and an exponent (in that order), computes the value of the base raised to the exponent, and prints the resulting product to stdout. See the stdio(3), stdin(3), stdout(3) scanf(3), and printf(3) manpages for help.

  48. Write a C program which accepts two integers as command line arguments, a base and an exponent (in that order), computes the value of the base raised to the exponent, and prints the resulting product to stdout.

  49. Write a C program which accepts three files as command line arguments. The first file given at the command line contains only the base while the second contains only the exponent. The program computes the value of the base raised to the exponent, and prints the resulting product to the file given by the third-command line argument.

  50. Write a C program with the same requirements as the previous exercise but this one invokes the pow function in the math library to perform the computation. See the pow(3) manpage for help.
  51. Regular expressions and filters

  52. To match the strings "abc" and "abbbc" but not "ac", use the extended regular expression:
    1. /ab*c/
    2. /ab+c/
    3. /ab?c/

  53. To match social security numbers, use the regular expression:
    1. /[0-9]*/
    2. /[0-9]+/
    3. /[0-9]\{9\}/

  54. (true or false) The shell metacharacter . and the grep metacharacter . have different semantics?

  55. (true or false) vi and emacs are qualitatively different in that vi has modes and emacs is modeless.

  56. (courtesy exercise 3-3, pp. 79 of [KP]) Create a file called grepfile, consisting of the ten lines of exercise 3-3 in K&P (p. 79), one grep command per line. Alternatively, you may copy grepfile from ~sperugin/text/grepfile. grepfile is available from ...
    grep \$
    grep \\$
    grep \\\$
    grep '\$'
    grep '\'$'
    grep \\
    grep \\\\
    grep "\$"
    grep '"$'
    grep "$"
    
    Give and explain the output of the following Korn commands:
    grep -n "\$" grepfile
    grep -n '\$' grepfile
    grep -n \\\\ grepfile
    grep -n '$' grepfile
    echo 'Go $HOME'
    echo "$5.00 is too much!"
    echo $(who | wc -l) users is not very many
    
  57. Programming shell filter scripts

    For the follow three questions do not use tools such as awk, Perl, Python, or any other scripting language.

  58. Write a complete Korn shell script which prints to stdout all lines of its single file argument which contain more than one word (any string of characters except whitespace).

  59. Write a complete Korn shell script which prints to stdout the lines of its single file argument which consist totally of 5-letter (upper and lower case) palindromes. A palindrome is a word which reads the same backwards and forwards (e.g., CbXbC)

  60. Write a complete Korn shell script which prints to stdout the list of files in the current directory (one per line), together with their date of late modification (use "date filename" format).

  61. Write a complete Korn shell script with the exact the same output as the preceding script, but this one invokes awk.

  62. Suppose the output of ls -l appears as follows (from p. 13 of [KP]), with spaces being the field separators.
    $ ls -l
    total 2
    -rw-r--r-- 1 you	19 Sep 26 16:25 junk
    -rw-r--r-- 1 you	22 Sep 26 16:26 temp
    
    Write a complete Korn shell script which prints to stdout the list of files in the current directory (one per line), together with their date of last modification (use `monthboxddboxfilename' format).
    1. without awk
    2. with awk

  63. The following is the bundle script from p. 98 of [KP]
    $ cat bundle
    # bundle:	group files into distribution package
    
    echo '# To unbundle, sh this file'
    for i
    do
       echo "echo $i 1>&2"
       echo "cat >$i <<'End of $i'"
       cat $i
       echo "End of $i"
    done
    $
    
    Suppose we have the following two files, ab and xyz:
    $ cat ab
    hello
    good
    bye
    $ cat xyz
    Abc
    Xyz
    
    1. What would be the stdout of the command line: $ bundle ab xyz
    2. In the bundle program, why is the first occurrence of End of $i single-quoted?

  64. Suppose we have a file ~/alongfile containing many misspelt words, including duplicates. Write a command line that would print to stdout a count of the misspelt words excluding duplicates.

  65. What would the following command lines do:
    1. $ grep '\^[^x]' y
    2. $ sed 's/\*f/g/' h

  66. Write a command line which would print to stdout only
    1. lines 50-100 of a file mnop.
    2. all lines in a file mnop which have only five characters in them and read the same backwards as forwards (i.e., five-character palindromes).
    3. all the lines in the files f1 and f2 which end with the literal string $HOME.

  67. Write a command line which tests if f1 is a directory.

  68. Consider the following ksh statements (assume that they are executed in the order that they are given and that the current directory is /home/lucy):
    10 $ foo=null
    11 $ print $foo
    12 $ foo="$foo set"
    13 $ print $foo
    14 $ set -A x $foo
    15 $ print ${x[1]}
    16 $ unset foo
    17 $ print ${foo:-unset}
    18 $ integer pwd=3
    19 $ if [[ ${pwd} = $(pwd) ]] ; then
    20 > print 3
    21 > else
    22 > print $(( pwd*2 )) ; fi
    23 $ A=quoted
    24 print "A '$(print $A)' \$ and escaped \."
    
    1. What is printed by statement 13?
    2. What is printed by statement 15?
    3. What is printed by statement 17?
    4. What do statements 18-22 output (a syntax error is a valid answer)?
    5. What is printed by statement 24?

  69. Consider the following Korn shell script printargs.
    #!/bin/ksh
    for arg in "$@"; do
       print $arg
    done
    
    What do each of the following command lines print?
    1. $ printargs a "b c" d
    2. $ printargs 'a "b c" d'

  70. Suppose we have a directory with 100 .c (C source) files. Write a complete Korn shell script which when invoked replaces the .c extension, on every file in the current directory which contain it, with .cpp.

  71. Consider the following Korn shell script (ls -t prints to stdout the names of the files in the current directory sorted by modification time):
    #!/bin/ksh
    
    ls -t | sed '/^'$1'$/q'
    
    return 0
    
    Explain what this does.

  72. Consider the following:
    $ cat ids
    111-22-4555
    254-34-2341
    314-34-4311
    314-57-0001
    701-09-1008
    ...
    

    ids is in the current directory and contains only valid social security numbers, one per line, with no leading or trailing whitespace.

    Write a command line to convert each line in the form xxx-yy-zzz to xxxyyzzz and print the results numerically sorted to stdout.

  73. Consider the following:
    $ cat names
    Brown, Michael
    Lowgator, Linus
    Perry, Lucy
    Caroll, Lewis
    Austin, Jane
    ...
    
    names is in the current directory and contains only one name (last,boxfirst) per line, with no leading or trailing whitespace.
    1. Write a command line, which does not invoke awk, to convert each line in the form (last,boxfirst) to (firstboxlast) and print the results alphabetically sorted to stdout.

    2. Write a command line, this time invoking awk, to convert each line in the form (last,boxfirst) to (firstboxlast) and print the results to stdout.
  74. Make and RCS

  75. What does the acronym RCS expand to?

  76. (true or false) RCS is a collection of Unix tools/commands for software project management.

  77. (true or false) In a Makefile line, leading tabs and spaces are insignificant.

  78. Consider the following:
    $ ls -l
    total 89
    -rw-------  1 lucy  users    196 Jun 25 09:41 Makefile
    -rw-------  1 lucy  users  90001 Jun 25 09:42 fig1.eps
    -rw-------  1 lucy  users      8 Jun 25 09:43 final.aux
    -rw-------  1 lucy  users  11056 Jun 25 09:43 final.dvi
    -rw-------  1 lucy  users   3664 Jun 25 09:43 final.log
    -rw-------  1 lucy  users  64411 Jun 25 09:44 final.ps
    -rw-------  1 lucy  users   8319 Jun 25 09:42 final.tex
    $ cat Makefile
    src = final.tex
    
    all: final
    
    final: final.ps
    
    final.ps: final.dvi
            dvips -o final.ps final
    
    final.dvi: ${src} fig1.eps
            latex final
    
    clean:
            touch *.tex
            rm final.log final.aux final.dvi final.ps
    $
    
    Which commands, if any, do the following commands force to execute? Consider the following two commands independently, i.e., the second is not run after the first.
    1. $ make final
    2. $ make
  79. More File manipulation, and Korn Shell

  80. Write a command line to make (only) each plain file (not directories or links) ending in .txt in or below your login directory readable by you and writable by you and others, without giving any extraneous permissions.

  81. Write a command-line to remove (only) all plain files (not directories or links) ending in .core residing in or below your login directory.

  82. Write a command-line to remove (only) all plain files ending in .core (only) residing in your current working directory.

  83. Write a command-line to make (only) each directory (not plain files or links) named abc or acc residing in or below your current working directory readable by you and your group; writable by you; and searchable by you, your group, and everyone, without giving any extraneous permissions.

  84. Give one advantage and one disadvantage of ar.

  85. Give one advantage and one disadvantage of tar.

  86. Write a command line to tar and gzip (only) all the files (plain files, directories, or links) ending in .c in or below ~/C into C.tgz in one stroke.

  87. Take the shell facilities described in the first chapter of K&P.

  88. How would you setup your environment such that files are created readable by you, your group, and others; and writable by you and your group, without giving any extraneous permissions, in such a way that this change would be in effect each time you logged in?

  89. Suppose you have a file $HOME/a/bfile. How would you arrange it, without giving any extraneous permissions, so that bfile would be readable by you and your group, writable by you, and executable by others?

  90. Give one example of file information which is in its parent directory.

  91. What is the minimum number of links to the directory d in the following figure, if circles represent directories and rectangles non-directory files? Explain.




  92. Explain the following transcript. Assume a plain file des exists in the current working directory.
    $ cat des
    process patterns building
    large scale systems
    using object technology
    $ ln des ~/b
    $ rm des
    $ cat ~/b
    
  93. (courtesy exercise 1-7, pp. 32-33 of [KP]) Explain the difference between
    $ who | sort
    
    and
    $ who > sort
    
  94. (courtesy exercise 2-1, pp. 45 of [KP]) What happens when you type <ctrl-d> to ed? Compare this to the following command:
    $ ed < file
    
  95. (courtesy exercise 2-4, pp. 52 of [KP]) du was written to monitor disk usage. Using it to find files in a directory hierarchy is at best a strange idiom, and perhaps inappropriate. As an alternative, look at the manual page for the find command, and compare the two commands. In particular, compare the command du -a | grep ... with the corresponding invocation of find. Which runs faster and how do you know? Is it better to build a new tool or use a side effect of an existing tool?

  96. (courtesy exercise 2-6, pp. 62 of [KP]) Why does ls -l report 4 links ot recipes? Hint: try
    $ ls -ld /usr/you
    
    Why is this useful information?

  97. (courtesy exercise 2-7, pp. 62 of [KP]) What is the difference between
    $ mv junk junk1
    
    and
    $ cp junk junk1
    $ rm junk
    
    Hint: make a link to junk, then try it.

  98. Choose any file in /dev on our system. The fourth section of the Unix Reference Manual on our system has descriptions of special files. Use it to give a brief description (paraphrase the manual) of the file you've selected. You may need to abbreviate the name of your file when you invoke man.

    If the file you've selected is a "symbolic link," which it probably is, follow it to an "original." Give the result of the ls -l command on that file, and explain the fields. Does its access list begin with a -, d, or l? Explain. Click here for an example file (do not use any contents from this in your solution). Your solution must take the form of this sample and provide a commensurate level of detail.

  99. (courtesy exercise 3-14, pp. 94 of [KP]) Compare the here-document version of 411 with the original. Which is easier to maintain? What is a better basis for a general service.


Return Home