Skip to main content
Logo image

Dive Into Systems: Exercises

Section 1.1 Getting Started Programming in C

Exercise 1.1.1. Variable and Type Determination in C and Python.

    Which of the following statements are true about variable and type determination in C and Python?
  • In C, variables must be explicitly declared with their data types, while in Python, variables are dynamically typed and their data types are determined at runtime.
  • Correct!
  • Both C and Python require explicit variable declarations with predefined data types.
  • Incorrect. C requires explicit variable declarations while Python does not.
  • In Python, variables are declared using the "var" keyword, while C uses the "int" keyword for integer variables and "char" for character variables.
  • Incorrect. The "var" keyword is used in other languages like Pascal, not Python!

Exercise 1.1.2. Running C and Python Programs.

    Which of the following statements are true about running C and Python programs?
  • Both C and Python code are interpreted line-by-line during execution, eliminating the need for compilation.
  • Incorrect.
  • C code needs to be compiled into a binary executable before it can be run, while Python code is interpreted line-by-line during execution.
  • Correct! This is a fundamental difference between C and Python programs!
  • C code is compiled into Python bytecode, which is then executed by the Python interpreter.
  • Sorry, incorrect! Did you know that Python was originally implemented in C?
  • Python code is compiled directly into a binary executable, similar to C.
  • Incorrect!

Exercise 1.1.3. Compiler Advantages.

    C code must be compiled before it is executed. Which of the following are advantages of requiring a compiler to compile your code before execution?
  • Compiled code is transformed into machine code that runs more efficiently on the target platform.
  • Correct!
  • Compilers analyze code and apply various optimizations during the compilation process.
  • Correct!
  • Once code is compiled, the resulting binary can be executed on the target platform without needing access to the source code.
  • Correct!
  • Once compiled, the resulting binary executable can run on any architecture.
  • A compiler targets a specific architecture, producing a binary executable that is designed to run on that architecture.

Exercise 1.1.4. Translating Python into C.

Rewrite the following Python program into C:.
def main:	    
     result = 1 + 2
     print("%d" %(result))

main()
Answer.
See below for the solution:
int main(void) { 
    int result;

    result = 1 + 2;
    printf("%d\n", result);

    return 0;
}