230 exam 1
The following code is written inside the main function. What is the output of the print command? HINT: look up man 3 strrchr! char str[10] = "hello"; char *p = strrchr(str,'l'); printf("%c\n", *(++p));
"o"
The -----format specifier should be used to print the value of the integer variable x that contains value 3 such that it has a field width of 3 and is right-aligned. NOTE: Include the % sign in your answer.
%3d
The ----- operator gives the address of a variable. NOTE: Specify only the symbol/character.
&
What character terminates C strings?
'\0'
What is the formula for calculating the mask used to extract the value of the bits L to R from an integer x?
((1 << (L - R + 1)) - 1) << R
What are the values of i and j after executing the following statements: (1): i=1;j=i++; (2): i=1;j=++i;
(1) i==2, j==1; (2) i==2, j==2
The XOR of an n-bit number and its one's complement is...
(2^n)-1
In a 32-bit CPU machine, what is the range of a signed int?
-2^31 to +2^31 - 1
What is the smallest negative number (that is, largest in magnitude) that can be represented by a signed 4-bit integer in one's complement representation?
-7
Which option do you provide to gcc to have it end compilation after the preprocessor?
-E
Which option do you provide to gcc to have it produce an assembly code file (as opposed to a binary, etc.)?
-S
Consider the following Unix Command executed in an empty directory: $ mkdir tmp/foo/baz/bing When executed as above the command fails. To allow directories to be created recursively you must use the --------
-p
If you initialize an array like int myArray[10] = { 1, 2 }; What is the value of myArray[2]?
0
What is the output of this C code? #include <stdio.h> int main() { int x = 0; int *ptr = &x; printf("%d\n", *ptr); }
0
01101001base2 & 01010101base2
01000001
a = 11010base2, b = 01011base2. What is a & b?
01010
What is the value of this expression? 0xBE && 0x12
0x1
What is 01101b + 01110b?
0x1B
What is 0xC08b8+ 0x7D5?
0xC108D
Consider the following C code: int x = 30, y = 0; x = !x; y = !x && !y; What is the value of y after executing the above statements?
1
What is the output of the following C code? #include <stdio.h> int main() { int x = 1, y = 0; int z = (y++) ? 3 : y == 1 && x; printf("%d\n", z); return 0; }
1
The binary fractional value of 1.75, before and after rounding using the default mode 'round-to-even', are:
1.11 and 10.00
Arrange the following in increasing order of data access time where 1 is the fastest access time and 7 is the slowest.
1.CPU registers 2.L1 Cache 3.L2 Cache 4.RAM 5.SSD 6.HDD
The Central Processing Unit (CPU) executes instructions stored in the memory. Which of the following activities does the CPU perform?
1.Extract a word from the instruction and copy that word into the program counter (PC). 2.Perform an arithmetic operation on the contents of two registers, and store the result in a register. 3.Store a byte or word from a register to a location in main memory. 4.Load a byte or word from main memory into a register.
Which of the following accesses the third element of array R?
1.R[2] 2.2[R] 3. *(&R[0] + 2)
a = 11010base2, b = 01011base2. What is a ^ b?
10001
0100 * 0011
1100
0110 + 0110
1100
What is the binary value of 105 base10
1101001
a = 11010base2, b = 01011base2. What is a | b?
11011
a = 11010base2. What is ~a? Assume a is an 8 bit value.
11100101
The largest four digit unsigned binary number that can be represented is:
1111
Consider the following 8-bit binary number in two's complement representation: 10010111 What do the bits look like after an arithmetic right-shift by 7 on the bits above? (Your answer should be 8 bits.)
11111111
What is the size in bytes of the following struct on a 32-bit machine? struct Mystery { char x; int y; char z; }
12 bytes
The binary value 10010100 is equivalent to:
148 and -108
Consider the following struct definition: struct s { int a; char *p; }; struct s *s1; On a 32-bit system the operation s1 += 2 increments the value of s1 by what number of bytes? You can assume that all types are aligned on a 4-byte boundary.
16
Consider the following C code: char x = 0x4; char y = x >> 1; What is the value of y after this code is executed?
2
In the following code, what is the size of *ptrs (assuming a 32-bit machine)? short s; short *ptrs; ptrs = &s; *ptrs = 10;
2 byte
Consider the following C code. What is the output when the program is executed? #include <stdio.h> void my_func(void) { int a = 2; printf("%d, ",a); } int main() { int a = 3; my_func(); printf("%d\n",a); return 0; }
2,3
What is the output of the following program? int main() { int x; int *y = &x; x = 10; *y = 20; printf("%d\n", x); }
20
What is the value of x in the following code? int a = 10; int *p = &a; int x = *p + 10;
20
The decimal and binary representations of the number 0XE4 is:
228, 11100100
Consider the following statements. int num = -1; int num_new = 0; num_new = ((unsigned)num << 24) >> 24; The value of num_new is------ NOTE: Enter integer value
255
Which of the following indicates a long integer constant in C?
255L
For an n bit signed integer, the two's complement representation of a negative integer x is the same as subtracting the absolute value of x from what value?
2^n
For an n bit signed integer, the two's complement representation of a negative integer x is the same as which of the following?
2^n - |x|
What is the value of x in the following code? int a = 10; int *p = &a; *p = 20; int x = *p + 10;
30
Consider the following C code, what is the output when the program is executed? #include <stdio.h> int main() { int a = 3; if(1) { int a = 4; if (1) { int a = 6; } printf("%d\n",a); } }
4
Consider the statement double ans = 10.0+2.0/4.0-2.0*3.0; What is the value of ans?
4.5
What is the output of the following C program on a 32-bit machine? #include <stdio.h> struct temp { int a[10]; char p; }; int main() { struct temp t1; printf("Size of a, p, and t1 are %d,%d,%d\n", sizeof(t1.a), sizeof(t1.p), sizeof(t1)); }
40,1,44
What is the decimal value of 00101001 base2
41
Consider a character variable a and a character constant 'x'. Then sizeof(a) + sizeof('x') =--------. (Assume a 32-bit system)
5
Convert 107base10 to hexadecimal. (Hint: Consider converting to base 2 and then to hexadecimal.)
6B
What is the largest positive number that can be represented by a signed 4-bit integer in two's complement representation?
7
How are C strings and arrays related?
A C string is an array of bytes.
What does this function copy do? typedef struct { char *ptr; } test; void copy (test *a, test *b) { int len = strlen(a->ptr) + 1; b->ptr = malloc(len); memcpy(b->ptr, a->ptr, len); }
A deep copy from a to b.
#include is called:
A preprocessor directive
What is a process?
Abstraction for a running program
Describe the differences between Java and C
Ad lib mofo
Consider this C function. int updateMyField (int x, int fld) { int mask = 0b1111; x &= ~(mask << 15); x |= (fld & mask) << 15; } Assuming a little-endian numbering, which bits of x does this code update with low order bits from fld?
Bits 15 through 18
Consider the following C code. int extractMyField (unsigned int x) { return (x << 4) >> 27; } Assuming a little-endian numbering of bits, which bits of x does this function return?
Bits 23 through 27
In the following function, is x passed by value or by reference? int someFunc(int* x, int y)
By Value
Consider the following C code, what is the output when the program is executed? #include <stdio.h> void m(); void foo() { m(); } int main() { void m() { printf("function-m"); } }
Compile time error
What is difference between concurrency and parallelism?
Concurrency is illusion of simultaneous program execution provided by context switching (CPU switching between programs) while parallelism is simultaneous programs executing on multiple cores.
How is a character represented by UNIX?
Each char is a byte
What is the output of the following code? #include <stdio.h> int main() { const int k = 5; k++; printf("k is %d\n", k); return 0; }
Error because a constant variable cannot be changed.
What is the major difference between a union and a struct?
Every field in a union occupies the same block of memory
A compiled C program will always auto-initialize an array to 0 for you.(T/F)
False
An #include directive is part of the C language. (T/F)
False
Consider the following sequence of Unix commands executed from the command line in an empty directory: $ mkdir tmp $ mkdir tmp The first execution of the mkdir command will create a new directory called tmp. The second execution of the mkdir command will delete the tmp directory and re-create it.(T/F)
False
If you loop off the end of a C array an exception is thrown.(T/F)
False
In two's complement representation of a 4-bit number, positive zero is represented by 0000 and negative 0 is represented by 1000 (T/F)
False
The following has no compilation errors. #include <stdio.h> int main() { float total; total = sum (2, 3); printf ("Total is %f\n", total); return 0; } float sum(float a, float b) { return a + b; } (T/F)
False
The main memory (RAM) provides permanent storage of data even after the machine is switched off. (T/F)
False
What does the following C code do? #include<stdio.h> int main() { unsigned num; scanf("%d", &num); int count = 0,max = 0; int i; for (i = 0; i<32; i++) { while (num & (1 << i)) { count++; if (count >max) { max = count; } i++; } count = 0; } printf("%d", max); return 0; }
Find the maximum number of consecutive 1s in an unsigned integer.
For the following c code, what is the output? #include <stdio.h> int main() { int x = 7, y = 6; int z = x /= y %= 6; printf("%d\n", z); return 0; }
Floating point exception
Make basics: describe each term/phrase simple2: simple2.c gcc simple2.c -o simple2 target: dependencies [tab] system command
From command line: make (target) will pattern match the Makefile where commands reside dependencies are what the build requires systems commands are what to do with the dependency
What is the scope of a function?
From the point of declaration to the end of the file being compiled
Consider the following C code, assuming a and b are integers: a = a ^ b; b = a ^ b; a = a ^ b; What does this code do? HINT: I urge you to search the web to figure out the answer to this one. Not only that - you should understand why.
It swaps a and b
Consider the following code snippet, union u { int a; char b; }; union u u1; u1.a = 4; u1.b = 'a'; int eq = (u1.a == u1.b); Which of the following is true about the above snippet?
Member variables a and b of u1 share the memory allocated for u1
Which of the following is false? a. The 'const' keyword is used to define constant values. b. Global constant variables are initialized to zero. c. Constant variables can be declared without assignment. d. None of these e. A constant variable cannot be reassigned.
None of these
(+) + (+) = (-)
Overflow
What does the following procedure do? void foo(unsigned int x) { for (int n = 32; n != 0; --n) { printf("%d", x & 1); x >>=1; } printf("\n"); }
Print the binary representation of x backwards
What does the code do? public ListNode foo(ListNode head) { A. Reverse a singly linked list. ListNode pre = null; B. Sort a singly linked list. while(head!=null) { C. Rotate a singly linked list by 1 place. ListNode temp = head.next; D. Remove a node from a linked list. head.next = pre; pre = head; head = temp; } return pre; }
Reverse a singly linked list.
How are files represented by UNIX at the lowest level?
Sequence of bytes
Which of the following is most true about the C code below? Assume it is compiled with the -std=c99 option of gcc. #include <stdio.h> int main() { int a[10]; for (int i = 0; i < 10; i++) { printf("%d\n", a[i]); } }
The program prints out garbage (unpredictable) numbers
Why use strncpy instead of strcpy?
To avoid data corruption and buffer overflow errors
Why does the compiler use struct padding?
To improve the program's efficiency
Where does the Operating System reside?
Top layer of systems programs, it is in between User Applications and Hardware
A single bitwise left shift of an integer is the same as multiplying the number by 2.(T/F)
True
Assuming the following declaration, int a[10]; then the value of a == &a[0]. (T/F)
True
In C (on most platforms), an unsigned character can be used to store unsigned integer values that fit in 8 bits. (T/F)
True
In any system (32/64-bit), the size of a character pointer == size of an integer pointer. (T/F)
True
On Unix (and Windows and OSX) a file is a sequence of bytes.(T/F)
True
Which of the following operations is illegal on a structure?
Typecasting a structure
(-) + (-) = (+)
Underflow
Can Virtual box run multiple Operating Systems?
Yes, each OS is a just a file stored in memory
What is *not* true about VirtualBox?
You cannot run multiple operating systems at the same time using VirtualBox.
If you compile a C program without the -o option it produces an executable file with a special name. What is it and what is its historical name?
a.out, which means "output from assembler"
A-----is the smallest addressable unit of memory (in most modern computers, and as assumed by the C programming language).
byte
What Unix command can be used to output the contents of a specific file?
cat
Which one of the following correctly describes heap allocation in C?
dynamic allocation
gdb
gdb ./simple2 debugger
Which of the following Unix commands is used to list files, including those that begin with a dot (.)?
ls -a
Which of the following is true about malloc?
malloc returns a NULL pointer if no memory is allocated
the ----- Unix command is used to get documentation for other Unix commands. (It works on itself, too.)
man
Not 'free'ing memory that is dynamically allocated using malloc causes
memory leaks
On a Unix system, a compiler translates a source file to an -------
object file
------- is a software package that manages computer memory, concurrent programming, interaction between applications and hardware, and I/O communication in a computer system.
operating system
What is the result of int *p = (int*)0x1000; p += 4?
p == 0x1010
What is the output of this C code? #include <stdio.h> void main() { char *s = "hello"; char *p = s; printf("%p\t%p", p, s); }
p and s print the same address
Which part of compilation in a C program handles the #define directive?
preprocessor
Comment on the following declaration: int *ptr, p;
ptr is a pointer to integer, p is not
To insure sign extension when shifting right, use the --------- int data type in C. (Answer with respect to the behavior of gcc on the x86 platform.)
signed
Suppose you have a file "test1" and another file "test2" and you type the following command: cat test1 >> test2. Which of the following is true?
test1 is appended to test2
Why can the switch statement in C (and other related languages) be so dangerous if not handled correctly?
the break statement is optional, making fall-through dangerous
A process is an abstraction of a running program. A virtual machine such as VirtualBox is an abstraction of:
the entire computer
valgrind
valgrind ./simple2 can prevent memory leaks by providing heap summaries use -Wall to show all warnings
The return-type used in string operations can only be:
void, int, and (char *)