COMP 1070/101 Dr. H. Melikyan Part 1 UNIX Problem #1 1. You should ONLY share your password with your lab instructor, the operator, anyone, nobody 2.Which of the following would copy the file "/root/nccu /readme" to your home directory? cd /root/nccu /readme cp /root/nccu /readme. cp /root/nccu /readme mv /root cp /root/nccu /readme root 3.Linux and UNIX are word processo spreadsheets text editor operating systems 4. How would you copy the file "homework.txt" in your home directory to a MSDOS disk? cp ~ a: mcopy ~/homework.txt a mcp ~/homework.txt a copy ~/homework.txt a: 5. How could you mail the text file "message.txt" to user djohn mail djohn < message.txt mail djohn > message.txt message.txt < mail djohn mail message.txt to djohn 6. How would you give every user read and write access to your ~/public directory? chmod 777 ~/public chmod 755 ~/puiblic chmod ug+rwx ~/public chmod all + wrx ~/puiblic 7. What command would you use to remotely log into bambi.acc.nccu.edu? ftp bambi.acc.nccu.edu login bambi.acc.nccu.edu bambi.acc.nccu.edu telnet bambi.acc.nccu.edu 1. Write a command that displays short listing of files and directories in current directory. 2. Write a command that gives you a "long" listing of the files and directories in the current directory. 3. Using the "wc" utility, write a command that counts the number of files and directories in the current directory. 4. Write a command that lists all the C program files in the current directory. 5. Write a command to display the first 10 lines of the file called ~/classes/hw1. 6. Write a grep command that finds all entries beginning with A and ending with a 2 or a 5. Part2 C languge Problem #2 () ( True or False) 1. Any printable ASCII character can be used in an identifier 2. Boolean data type can be represented by integer values . 3. The left operand in assignment expression must be single variable 4. Associativity is used to determine which of several different operations is evaluated first 5. In C there are only three loop statements while, for and do.. while 6. The break statement can be used only inside of switch statement. 7. The continue and break statements can be used also inside of loop statements. 8. The function definition contains the code for function T F T F T F T F TF T F T F 9. The function call that returns void may not be used as a part of expression 10. The function declaration requires that the parameters be named 11. The function definition header concludes with a semicolon (;) 12. In function definition the parameter are known as actual parameters 13. Local variables are defined in the functions body 14. Local variables cannot be referenced by their identifiers from outside the function where they are defined 15 The types of elements in array must be same type 16. When array is passed to function, C uses pass by reference 17. A pointer variable is a variable which can store an address 18. Because all pointers are addresses, pointers of different types can be assigned without a cast 19. A pointer can be defined to point to another pointer variable (pointer to pointer) 20. A pointer constant is an address that cannot be changed T F T F T F T F TF TF TF T F TF T F TF TF Problem #3 Multiple selections 1. Which of the following identifiers is a valid ? 1-PTIONS, _OPTIONS, OPTIO%NS123, OPTI*ON, OPTIONIS_IS_NOTAVARIABLR 2. Which of the followings is not true for variables? a) the variables can have value, type, address, and name, b) the variables can be initialized only when are declared. c) the value of variable can be changed during of program execution. d) only integer values can be assigned to variables. 3. Which function reads data from the keyboard? main, readdisplay, write, scanf, printf 4. Which of followings shows address of variable x? $x, @x, #x, address(x), &x, 5. Which of the following is not a relational operator? a) <, b) =, c) >, d) <=, f) >= . 6. Which of the C loops is a pretest loop. a) do ..while b) for, c) while, d) both for and while . 7. Which of the following statements is not a jump statement? a) break, b) return, c) case, d) goto, e) continue. 8. Which one of the following is not valid assignment statement? a) x = 12; b) x - 12 = 23; c) x - = 10; d) x = y = 13; f) x+ = 18; 9. If a) b) c) originally x = 2, y = 5 and z = 12, what is the value of each of the following expressions? x++ + y++ the value of expression = ++x + --z the value of expression = x++ the value of expression = 10. If int x = 47; what is the value of each of the following expressions? a) x%15; answer = b) x/8; answer = c) (x/5)%3; answer = d) x/80. answer = 11. Evaluate the following expressions to true or false. a) !( 3 + 4 >= 5) answer = b) 1 + 6 = = 8 || 3 + 2 == 5; answer = c) 1 > 6 || 5&&12; answer = d) 12 != 7 && 12 <25; answer = 12. If x = 5 , y = 0, and z = -4, what is the value each of the following expressions? a) x||y&&z ; answer = b) (x&&y)||z ; answer = c) (x||y) && z; answer = d) x + y < x + y + z; answer = 13 Which of the following array initialization statements is valid? a) int array{} = {1, 2, 3, 4, 5}; b) int array[] = [1, 2, 3, 4, 5]; c) int array[6] = {1, 2, 3, 4, 5}; d) int array[4] = {1, 2, 3, 4, 5}; Problem #4 Exercises: 1. 2. Write the code that defines each of the followings. a) an integer variable sum initializes to 5, b) a floating point variable initialized to 0.3. c) a pointer variable ptr pointing to and integer. d) a pointer variable ptrch pointing to a pointer to char. Write a statement to print the following line. (Assume that variable comp_price shows actual price of computer 1445.99.) The sale price of computer is: $ 1445.99 3. Write a program that uses printf statement to print the following pattern of asterisks. **** **** **** 4. What is the output of the following code fragment? int a,b; a= b= 25; a*= 2; b/= 5; printf(“ The output is : %d\n%d \n”, a, b ); 5. If originally x = 0, y= 4, and z = 3 what is the values of x, y, and z after executing the following code? a) if(++y) if(z++ ) if (y) z = 10; else y = z-3; Answer: x = y= z= b) if ( y = x) { y++; z-- ; } else --x; Answer: x = y= z= switch( x){ case 1: x = 2; y = 5; break; case 0: case 2: x = - 4; y = 12; default: z = 16; } Answer: x = y= z= c) 6. What would be printed from each of the following program segments? a) int x = 10; Answer: while( x > 8){ printf(“%d\n”, x); x--; } b) for ( x= 10; x > 6; x--) printf(“%d\n”, x); Answer: 7. Change the following while loops to for loops a) int x = 5; while ( x < 10){ printf(“%d\n”, x); x++; } 8. Use for loop to write a program that creates the following pattern. 1 12 123 1234 9. Write a program that prompts the user to input three integers. As the numbers are read, the program computes the sum of this integers and result prints out. Problem #5 a) Fill in the blank with the appropriate code, b) After filled in, what gets printed? c) Is "junk" in fun2() same as in main()? d) List the local variables e) Edit the program so main() is above the fun1 and fun2 function definitions for ANSI C f) Edit the code if "j" in fun2() was a double value of 1.5 and now prints in the last line "5.5" ----------------------------------------------------------#include <stdio.h> __________ fun1(int junk) { junk *= 2; } __________ fun2( __________ ) { int j = 1, junk = 4; fun1(junk); return(junk + j); } void main( void ) { int junk = 3; fun1( junk ); printf("%d \n", junk); printf("%d \n", fun2() ); } Problem#6 Pointers and arrays a) What gets printed? ------------------------------------------------------------------#include <stdio.h> void fun( int *q ) { *q = 5; } void main( void ) { int me = 5, you = 3, *p; p = &me; printf("%d \n", *p); p = &you; you += me; printf("%d \n", *p); fun(&you); printf("%d %d \n", you, *p); Answer: Answer: Answer: b) What gets printed? -------------------------------------------------------------------- #include <stdio.h> void swap(int *p1, int *p2) { int tmp; tmp = *p1; *p1 = *p2; *p2 = tmp; } void main ( void ) { int i, a[ ]= {1, 2, 4, 5, 7, 9}; printf("%d %d %d\n", *a + 4, *(a + 4), a[6] ); swap(&a[0], &a[2]); printf("%d %d %d\n", a[0], a[1], a[2] ); } Answer: Answer: