Computer Programming 1 - Loops, Arrays, and Strings
48 Questions
5 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What does the function strcat(str1, str2) do?

  • It copies str2 into str1.
  • It compares two strings.
  • It checks if str1 and str2 are equal.
  • It combines str1 and str2 into a single string. (correct)
  • What is the output when strcmp(str1, str2) is called and both strings are equal?

  • 0 (correct)
  • Undefined value
  • -1
  • 1
  • Which header file is required to use the printf() function?

  • string.h
  • stdlib.h
  • math.h
  • stdio.h (correct)
  • Which function would you use to read user input from the console?

    <p>scanf()</p> Signup and view all the answers

    What does sqrt() function compute?

    <p>The square root of a number.</p> Signup and view all the answers

    What is the purpose of using fgets in string input?

    <p>It reads a string until the buffer is full.</p> Signup and view all the answers

    If str1 is 'abc' and str2 is 'abc', what will strcmp(str1, str2) return?

    <p>0</p> Signup and view all the answers

    In the given code snippet, which of the following is an example of array initialization?

    <p>int values[] = {10, 20, 30, 40, 50};</p> Signup and view all the answers

    Which of the following is NOT a standard library function?

    <p>customFunction()</p> Signup and view all the answers

    What is the behavior of strcat() if str1 is 'Hello' and str2 is 'World'?

    <p>'HelloWorld'</p> Signup and view all the answers

    What will happen if there is a whitespace between two strings while reading?

    <p>Only the first string will be read.</p> Signup and view all the answers

    What does the strncat function do?

    <p>It concatenates a specified number of characters.</p> Signup and view all the answers

    Which of the following is NOT a characteristic of the fgets function?

    <p>It can only read integers.</p> Signup and view all the answers

    What type of variable is numbers in the code snippet?

    <p>Integer</p> Signup and view all the answers

    Why is it important to initialize an array?

    <p>To ensure the elements have default values.</p> Signup and view all the answers

    What does the initialization int values[] = {10, 20, 30, 40, 50}; represent?

    <p>A declaration of an array with initialized values.</p> Signup and view all the answers

    What is the primary function of strlwr()?

    <p>To convert uppercase characters into lowercase</p> Signup and view all the answers

    What is the return type of a function that does not return a value?

    <p>void</p> Signup and view all the answers

    Which statement about user-defined functions is true?

    <p>They are designed to perform specific tasks</p> Signup and view all the answers

    What is the purpose of the strupr() function?

    <p>To convert lowercase characters into uppercase</p> Signup and view all the answers

    In the context of functions, what is meant by 'scope'?

    <p>The area of memory where variables are accessible</p> Signup and view all the answers

    How are parameters passed to functions?

    <p>By explicitly stating them when calling the function</p> Signup and view all the answers

    What is the primary use of a 'for' loop in C?

    <p>When the exact number of iterations is known beforehand</p> Signup and view all the answers

    In which programming scenario would a 'while' loop be most appropriate?

    <p>When user input can determine the iterations</p> Signup and view all the answers

    Which of the following describes a characteristic of local variables?

    <p>They are only accessible within the function where they are declared</p> Signup and view all the answers

    What is the purpose of the 'do-while' loop in C?

    <p>To execute the loop body at least once</p> Signup and view all the answers

    What is required in a function declaration?

    <p>The return type, function name, and parameters</p> Signup and view all the answers

    What does the 'break' statement achieve in loop control?

    <p>It exits a loop prematurely based on a condition</p> Signup and view all the answers

    What would be the output of a 'for' loop if set up to iterate 10 times without any additional code to process?

    <p>No output</p> Signup and view all the answers

    If a loop uses 'continue', what is the intended effect?

    <p>To skip the current iteration and proceed to the next</p> Signup and view all the answers

    Why is user input validation commonly associated with loops?

    <p>Loops ensure user inputs can be repeated until valid</p> Signup and view all the answers

    In C, what is a common mistake when writing 'for' loops?

    <p>All of the above</p> Signup and view all the answers

    What does the function strncat(str1, str2, n) accomplish?

    <p>It concatenates a specific number of characters from str2 to str1.</p> Signup and view all the answers

    How does strcpy(str1, str2) function primarily operate?

    <p>It copies str2 completely into str1.</p> Signup and view all the answers

    What does the function strlen(str) return?

    <p>The number of characters in the string excluding the null terminator.</p> Signup and view all the answers

    What is a key difference between strlen and sizeof?

    <p><code>sizeof</code> includes the null terminator when counting while <code>strlen</code> does not.</p> Signup and view all the answers

    What will strncpy(str1, str2, n) do?

    <p>It copies a specific number of characters from str2 to str1.</p> Signup and view all the answers

    Which function is used to print size_t values in C?

    <p>%zu</p> Signup and view all the answers

    What does %zu represent in C?

    <p>A format specifier for printing size_t values.</p> Signup and view all the answers

    What is the output of sizeof(str) when str is a character array?

    <p>It includes the null terminator in the character count.</p> Signup and view all the answers

    What is the purpose of the 'break' statement in loops?

    <p>To stop the loop's execution and transfer control to the statement after the loop.</p> Signup and view all the answers

    How does the 'continue' statement function within a loop?

    <p>It skips the rest of the current iteration and jumps to the next iteration of the loop.</p> Signup and view all the answers

    What is a string in C programming?

    <p>A sequence of characters stored in memory.</p> Signup and view all the answers

    What does the null terminator ' ull' indicate in a string?

    <p>The end of the string.</p> Signup and view all the answers

    What is the size of an integer in memory in C?

    <p>4 bytes.</p> Signup and view all the answers

    What happens when scanf encounters a whitespace character while reading strings?

    <p>It stops reading and terminates the string input.</p> Signup and view all the answers

    Which of the following correctly defines an array in C?

    <p>A fixed-size linear collection of similar type elements stored in contiguous memory.</p> Signup and view all the answers

    What is the correct way to declare a string variable in C?

    <p>char message[] = &quot;Hello&quot;;</p> Signup and view all the answers

    Study Notes

    Computer Programming 1 - Finals Coverage

    • Lesson V: Loops

      • while loop: Ideal when the exact number of iterations isn't known beforehand

      • do-while loop: Guarantees the loop body executes at least once

      • for loop: Used when the number of iterations is known upfront. Follows the format for (initialization; condition; increment/decrement) { ... }

    • Lesson VI: Arrays

      • Arrays are collections of elements of the same data type, stored contiguously in memory.

      • Array indices start at 0

      • Each element can be accessed using its index

      • One integer typically takes 4 bytes of memory

    • Lesson VII: Strings

      • Strings are sequences of characters

      • In C, strings are arrays of characters

      • Strings often end with a null terminator (\0)

      • Functions like scanf are used to read strings from input but stop at whitespace

      • fgets is preferred when reading strings containing spaces.

    • Lesson VIII: Functions

      • Functions are blocks of code designed to perform a specific task.

      • main Function: Execution begins here

      • User-defined functions: Write your functions

      • Standard library functions: Predefined functions (like printf, scanf, strlen, strcpy) requiring inclusion of appropriate headers (like stdio.h, string.h). Example printf() is from stdio.h.

      • Return Types: Functions can return values. Common return types including int, float, char, or double.

      • Void Functions: Functions that don't return a value are declared using the void keyword

    String Operations/Functions

    • String Length:

      • strlen(str): Returns the length of the string, excluding the null terminator
    • String Concatenation:

      • strcat(str1, str2): Appends str2 to str1

      • strncat(str1, str2, n): Appends up to n characters of str2 to str1

    • String Copying:

      • strcpy(str1, str2): Copies str2 to str1

      • strncpy(str1, str2, n): Copies up to n characters of str2 to str1

    • String Comparison:

      • strcmp(str1, str2): Compares str1 and str2; returns 0 if equal, a negative value if str1 is less than str2, and a positive value if str1 is greater than str2.
    • String Lowercase/Uppercase Conversion:

      • strlwr(str) converts a string to lowercase

      • strupr(str) converts a string to uppercase

    • Input: The fgets function is preferred for reading strings that may contain whitespace; scanf stops at whitespace

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Finals COMP002 Reviewer PDF

    Description

    This quiz covers essential concepts from Computer Programming 1, focusing on loops, arrays, and strings. It includes questions on the usage of while, do-while, and for loops, as well as array handling and string manipulation in C. Test your understanding and prepare thoroughly for your finals!

    More Like This

    Use Quizgecko on...
    Browser
    Browser