C Programming Practice Questions - Basics

"You cannot be great at the start, but you have to start to be great. So, making a beginning is important. In this list of questions, get your feet wet,before beginning a more arduous C journey"

— All the best

Print

    Algorithm Analysis Practice Problems

    I. Comparison and Suitability (Growth Rates)

    • Problem 1: Determine the Range of Suitability for Exponential vs. Polynomial Time.

      Task: Specify the range of $n$ for which Algorithm A ($3^n$) or Algorithm B ($n^4$) is more efficient.

    • Problem 4: Compare a Polynomial Function to a Log-Linear Function.

      Task: Find the approximate value of $n$ where the function $10n \log_2 n$ first becomes larger than $n^{1.5}$.

    • Problem 5: Determine Which Function Grows Asymptotically Faster (Polynomial Simplification).

      Task: Compare $n^2 + 5n$ and $\frac{n^3}{\sqrt{n}}$ to identify the function with the highest order of growth.

    II. Loop Analysis and Frequency Counts

    • Problem 2: Analyze the Time Complexity ($O$) of a Nested Loop with Variable Bounds.

      Task: Determine the Big $O$ notation for the following algorithm segment, where the inner loop iterates up to $i^2$.

      count = 0
      for i := 1 to n do
          for j := 1 to i * i do
              count = count + 1
          end for
      end for
                  
    • Problem 3: Calculate the Exact Frequency Counts for Simple and Logarithmic Loops.

      Task: For the two program segments below, calculate how many times the innermost statement (`x := x + 1;` or similar) executes as a function of $n$.

      Segment A:

      for i := 1 to 2 * n do
          for j := i to n do
              x := x + 1;
          end for;
      end for;
                  

      Segment B:

      i := 1;
      while i < n do
          x := x + 1;
          i := i * 2;
      end;
                  

    Point out the errors

    Question Statement Error? (Yes/No) Type of Error / Correction (Answer Slot)
    C.1 int area; radius = 5; area = 3.14 * radius * radius;
    C.2 float volume = (4/3) * pi * r * r * r;
    C.3 char initial = 'j';
    C.4 if (x = 10) { printf("X is ten"); }
    C.5 int total_amt, char name[20];
    • Problem 6: Sort a Set of Functions Based on Their Order of Growth.

      Task: List the following functions from the fastest-growing (highest order) to the slowest-growing (lowest order), identifying any that share the same asymptotic class.

      $$\sqrt{n} \quad n^2 \log n \quad 2^n \quad n! \quad \log (n^3) \quad n \quad 7n^3 - 10n^2 \quad (\log n)^2 \quad 1$$

    • Problem 7: Apply the Definitions of Big $O$ Notation ($f(n) = O(g(n))$ or $g(n) = O(f(n))$).

      Task: For each pair of functions $f(n)$ and $g(n)$, determine whether $f(n) = O(g(n))$, $g(n) = O(f(n))$, or both.

      (a) f(n) = n^2 + log n, g(n) = n^3
      (b) f(n) = 2^n, g(n) = n^100
      (c) f(n) = n \sqrt{n} + 5n, g(n) = n^2
      (d) f(n) = n^2 / 5, g(n) = n^2 + \sqrt{n}
      (e) f(n) = n \log n, g(n) = \sqrt{n} \log n
      (f) f(n) = 100, g(n) = log n
                  
    • For example, the output should look like this:
      What is this life if, full of care,
      we have no time to stand and stare.
      No time to stand beneath the boughs
      and stare as long as sheep or cows.
    • Write a program that prints the following direct statement using a single print function and only double quotes (" ").
    • For example, the output should look like this:
      She said to me,"I want to learn Python."
    • Print a the following message using a single print function and only triple quotes (''' ''').
    • Print a the following message using a single print function and only double quotes (""). No loops has to be used.
    • Write a program that prints a name 5 times vertically, with one name per line. Don't use loops.
    • For example, if the name is John, the output should look like this:
      John
      John
      John
      John
      John
    • Write a program that prints a name 5 times horizontally on a single line. Don't use loops.
    • For example, if the name is John, the output should look like this:
      John John John John John
    • Write a program that prints the title Elvin's book using a single print statement and single inverted commas (' ').
    • For example, the output should look like this:
      'Elvin's book'
    • Print the following pattern using format function within the print function:
    • Print the following pattern using format function within the print function:
    • Print a short poem you like, ensuring each line is on a new line.
    • Print a description of your dream vacation using both single and double quotes.
    • Print a message that uses emojis or special characters (like ♥️ or 😊) to express an emotion or idea.

    • Programming Questions

      Write a complete C program (including input and output statements) for each of the following problems.

      • Write a program that takes a temperature in Fahrenheit ($F$) as input from the user and converts it to Celsius ($C$)
      • Declare a variable called helpline_number to store the national emergency number of your country and print the message: "In any emergency, contact [helpline_number]."
      • Declare a variable to store the name of a country and a second variable called helpline_number to store the national emergency number. Print the message: "In any emergency in [country name], contact [helpline_number]."
      • Declare a variable to store a first number and a second variable to store a second number. Declare a third variable to store their sum and print it.
      • Declare a variable to store a first number and a second variable to store a second number. Declare four extra variables to store the sum, difference, product, and division of the two numbers and print them.
      • Assign the value "25" to a variable called age and print the message: "I am 25 years old."
      • Assign the values "25" and "Engineering" to variables age and major, respectively. Print the message: "I am 25 years old and studying Engineering."
      • Declare a variable city with the value "New York" and print the message: "I live in \'New York.\'"
      • Create three variables, city, state, and country, with the values "New York", "NY", and "USA", respectively. Print the message: "I live in New York, NY, USA."
      • Declare two variables, price and discount, with the values "100" and "20", respectively. Then, calculate and print the discounted price (price - discount).
      • Create three variables, num1, num2, and num3, with the values "10", "20", and "30", respectively. Calculate and print the average of the three numbers.
      • Declare a variable x with the value "5". Then, calculate and print the value of x squared (x^2).
      • Assign the value "10" to a variable y. Then, calculate and print the value of y multiplied by 3.
      • Create a variable z with the value "2". Then, calculate and print the value of z cubed (z^3).
      • Create three variables, e, f, and g, with the values "2", "3", and "4", respectively. Then, calculate and print the value of e multiplied by f plus g.
      • Write the C Language expression for the following:

        1. ( a + b ) 2 + c

        2. a 2 + b 2

        3. x3 + y3 + x y 3

        4. a2 + b2 a2 - b2

        5. z = ab + bc + ca 3abc

        6. z = ab + bc + ca 3abc

        7. a = 0.05 - 2 y 3 x - y

        8. ( a + b ) n 3 + b

        9. ax + by x 3 + y 3

Find it difficult?

Don’t lose heart, don’t be under confident, just be consistent in your preparation and be sure of everything you’ve studied. You can request a class so that we can help you understant this topic.

Feel Confident?

Your first step in learning any new topic is to be aware of your strengths and weaknesses. Once you know this, your self-preparation can be meaningful and result-oriented. Attempt an quiz to get tested.