Programming Basics Quiz
15 Questions
0 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 is the difference between low-level language and high-level language?

Low-level languages are closer to the machine's instructions, requiring detailed programming and often being specific to a particular processor. High-level languages are more human-readable and abstract, providing a more convenient way to develop software.

Write a program that reads a string from the user using gets() and prints it using puts().

#include <stdio.h>

int main() {
  char str[100];

  printf("Enter a string: ");
  gets(str);

  printf("You entered: %s\n", str);

  return 0;
}

Define a nested structure named 'Manager' to store information about a manager, including their ID, name, department, salary, and address. The address should have fields for building name, flat number, and city. Write a program to display all these details.

#include <stdio.h>

struct Address {
  char building_name[50];
  int flat_number;
  char city[50];
};

struct Manager {
  int id;
  char name[50];
  char department[50];
  float salary;
  struct Address address;
};

int main() {
  struct Manager manager;

  // Input manager details
  printf("Enter Manager ID: ");
  scanf("%d", &manager.id);
  printf("Enter Manager Name: ");
  scanf("%s", manager.name);
  printf("Enter Manager Department: ");
  scanf("%s", manager.department);
  printf("Enter Manager Salary: ");
  scanf("%f", &manager.salary);
  printf("Enter Building Name: ");
  scanf("%s", manager.address.building_name);
  printf("Enter Flat Number: ");
  scanf("%d", &manager.address.flat_number);
  printf("Enter City: ");
  scanf("%s", manager.address.city);

  // Display manager details
  printf("\nManager Details:\n");
  printf("ID: %d\n", manager.id);
  printf("Name: %s\n", manager.name);
  printf("Department: %s\n", manager.department);
  printf("Salary: %.2f\n", manager.salary);
  printf("Address:\n");
  printf("Building Name: %s\n", manager.address.building_name);
  printf("Flat Number: %d\n", manager.address.flat_number);
  printf("City: %s\n", manager.address.city);

  return 0;
}

Write an algorithm and draw a flowchart to calculate the perimeter and area of a rectangle given its length and width.

<p><strong>Algorithm:</strong></p> <ol> <li>Start</li> <li>Get the length and width of the rectangle.</li> <li>Calculate the perimeter: Perimeter = 2 * (length + width)</li> <li>Calculate the area: Area = length * width</li> <li>Display the perimeter and area.</li> <li>Stop</li> </ol> <p><strong>Flowchart:</strong> [A flowchart depicting the steps mentioned in the algorithm can be drawn here.]</p> Signup and view all the answers

Which of these are considered programming tokens in C?

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

Write a C program to determine if a given string is a palindrome or not.

<pre><code class="language-c">#include &lt;stdio.h&gt; #include &lt;string.h&gt; int main() { char str[100]; int i, j, isPalindrome = 1; printf(&quot;Enter a string: &quot;); gets(str); i = 0; j = strlen(str) - 1; while (i &lt; j) { if (str[i] != str[j]) { isPalindrome = 0; break; } i++; j--; } if (isPalindrome) { printf(&quot;%s is a palindrome.\n&quot;, str); } else { printf(&quot;%s is not a palindrome.\n&quot;, str); } return 0; } </code></pre> Signup and view all the answers

Explain the difference between call by value and call by reference with examples.

<p><strong>Call by Value:</strong> In call by value, a copy of the argument is passed to the function. Any modifications made to the argument inside the function do not affect the original variable.</p> <p><strong>Example:</strong></p> <pre><code class="language-c">void swap(int a, int b) { int temp = a; a = b; b = temp; } int main() { int x = 10, y = 20; swap(x, y); printf(&quot;x: %d, y: %d\n&quot;, x, y); // Output: x: 10, y: 20 return 0; } </code></pre> <p><strong>Call by Reference:</strong> In call by reference, the address of the argument is passed to the function. Any modifications made to the argument inside the function directly affect the original variable.</p> <p><strong>Example:</strong></p> <pre><code class="language-c">void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int x = 10, y = 20; swap(&amp;x, &amp;y); printf(&quot;x: %d, y: %d\n&quot;, x, y); // Output: x: 20, y: 10 return 0; } </code></pre> Signup and view all the answers

Write a program to calculate the sum and average of elements in an array.

<pre><code class="language-c">#include &lt;stdio.h&gt; int main() { int arr[100], n, i, sum = 0; float average; printf(&quot;Enter the number of elements: &quot;); scanf(&quot;%d&quot;, &amp;n); printf(&quot;Enter %d elements: &quot;, n); for (i = 0; i &lt; n; i++) { scanf(&quot;%d&quot;, &amp;arr[i]); sum += arr[i]; } average = (float)sum / n; printf(&quot;Sum of the array elements: %d\n&quot;, sum); printf(&quot;Average of the array elements: %.2f\n&quot;, average); return 0; } </code></pre> Signup and view all the answers

Explain the data input and output functions used in C.

<p>Input/output functions are essential for interacting with the user and external devices in C. Key functions include:</p> <ul> <li> <strong><code>scanf()</code>:</strong> Reads data from the standard input stream (usually the keyboard) and stores it in the specified variables.</li> <li> <strong><code>printf()</code>:</strong> Prints formatted output to the standard output stream (usually the console).</li> <li> <strong><code>gets()</code>:</strong> Reads a string of characters from the standard input stream, stopping at a newline character.</li> <li> <strong><code>puts()</code>:</strong> Prints a string of characters to the standard output stream, followed by a newline character.</li> <li> <strong><code>getchar()</code>:</strong> Reads a single character from the standard input stream.</li> <li> <strong><code>putchar()</code>:</strong> Prints a single character to the standard output stream.</li> </ul> <p>These functions provide the means to take data from the user, display results, and interact with the user in various ways.</p> Signup and view all the answers

Explain the difference between an algorithm and a flowchart.

<p>An <strong>algorithm</strong> is a step-by-step sequence of instructions designed to solve a specific problem. It is a logical and structured process that outlines how to perform a task. A <strong>flowchart</strong>, on the other hand, is a visual representation of an algorithm. It uses symbols and arrows to depict the flow of control and the sequence of operations in the algorithm.</p> Signup and view all the answers

What is a function in C, and why is it needed? Write a program to determine if a number is prime or not using a function.

<p>In C, a function is a block of code that performs a specific task. Functions are crucial for organizing code, reusing code, and breaking down a complex program into smaller, manageable components. Here's a program to determine if a number is prime using a function:</p> <pre><code class="language-c">#include &lt;stdio.h&gt; int isPrime(int num) { if (num &lt;= 1) { return 0; // 0 and 1 are not prime } for (int i = 2; i * i &lt;= num; i++) { if (num % i == 0) { return 0; // Not a prime number } } return 1; // Prime number } int main() { int num; printf(&quot;Enter a number: &quot;); scanf(&quot;%d&quot;, &amp;num); if (isPrime(num)) { printf(&quot;%d is a prime number.\n&quot;, num); } else { printf(&quot;%d is not a prime number.\n&quot;, num); } return 0; } </code></pre> Signup and view all the answers

Write a program to implement a simple calculator with operations like addition, subtraction, multiplication, and division using a switch statement.

<pre><code class="language-c">#include &lt;stdio.h&gt; int main() { int num1, num2, choice; float result; printf(&quot;Enter two numbers: &quot;); scanf(&quot;%d %d&quot;, &amp;num1, &amp;num2); printf(&quot;Select operation:\n&quot;); printf(&quot;1. Add\n&quot;); printf(&quot;2. Subtract\n&quot;); printf(&quot;3. Multiply\n&quot;); printf(&quot;4. Divide\n&quot;); printf(&quot;Enter your choice: &quot;); scanf(&quot;%d&quot;, &amp;choice); switch (choice) { case 1: result = num1 + num2; printf(&quot;%d + %d = %.2f\n&quot;, num1, num2, result); break; case 2: result = num1 - num2; printf(&quot;%d - %d = %.2f\n&quot;, num1, num2, result); break; case 3: result = num1 * num2; printf(&quot;%d * %d = %.2f\n&quot;, num1, num2, result); break; case 4: if (num2 == 0) { printf(&quot;Division by zero is not allowed.\n&quot;); } else { result = (float)num1 / num2; printf(&quot;%d / %d = %.2f\n&quot;, num1, num2, result); } break; default: printf(&quot;Invalid choice.\n&quot;); } return 0; } </code></pre> Signup and view all the answers

Write a C program to find the smallest of three numbers using logical operators.

<pre><code class="language-c">#include &lt;stdio.h&gt; int main() { int num1, num2, num3, smallest; printf(&quot;Enter three numbers: &quot;); scanf(&quot;%d %d %d&quot;, &amp;num1, &amp;num2, &amp;num3); smallest = num1; if (num2 &lt; smallest) { smallest = num2; } if (num3 &lt; smallest) { smallest = num3; } printf(&quot;Smallest number: %d\n&quot;, smallest); return 0; } </code></pre> Signup and view all the answers

Write a program to perform matrix multiplication.

<pre><code class="language-c">#include &lt;stdio.h&gt; int main() { int mat1[10][10], mat2[10][10], result[10][10]; int rows1, cols1, rows2, cols2, i, j, k; printf(&quot;Enter the number of rows and columns of matrix 1: &quot;); scanf(&quot;%d %d&quot;, &amp;rows1, &amp;cols1); printf(&quot;Enter the elements of matrix 1:\n&quot;); for (i = 0; i &lt; rows1; i++) { for (j = 0; j &lt; cols1; j++) { scanf(&quot;%d&quot;, &amp;mat1[i][j]); } } printf(&quot;Enter the number of rows and columns of matrix 2: &quot;); scanf(&quot;%d %d&quot;, &amp;rows2, &amp;cols2); if (cols1 != rows2) { printf(&quot;Matrix multiplication not possible. Incompatible dimensions.\n&quot;); return 1; } printf(&quot;Enter the elements of matrix 2:\n&quot;); for (i = 0; i &lt; rows2; i++) { for (j = 0; j &lt; cols2; j++) { scanf(&quot;%d&quot;, &amp;mat2[i][j]); } } // Matrix multiplication for (i = 0; i &lt; rows1; i++) { for (j = 0; j &lt; cols2; j++) { result[i][j] = 0; for (k = 0; k &lt; cols1; k++) { result[i][j] += mat1[i][k] * mat2[k][j]; } } } printf(&quot;Resultant matrix:\n&quot;); for (i = 0; i &lt; rows1; i++) { for (j = 0; j &lt; cols2; j++) { printf(&quot;%d &quot;, result[i][j]); } printf(&quot;\n&quot;); } return 0; } </code></pre> Signup and view all the answers

Write a C program to determine if a given year is a leap year.

<pre><code class="language-c">#include &lt;stdio.h&gt; int main() { int year; printf(&quot;Enter a year: &quot;); scanf(&quot;%d&quot;, &amp;year); if ((year % 4 == 0 &amp;&amp; year % 100 != 0) || year % 400 == 0) { printf(&quot;%d is a leap year.\n&quot;, year); } else { printf(&quot;%d is not a leap year.\n&quot;, year); } return 0; } </code></pre> Signup and view all the answers

Study Notes

Low-Level vs. High-Level Languages

  • Low-level languages are closer to machine code, while high-level languages are easier for humans to understand.

String Input and Output

  • Write a program using gets() and puts() to take and print a string input from the user.

Nested Structures

  • Define a nested structure named "Manager" that stores ID, name, department, salary, and an address (with building name, flat number, and city).
  • Write a program to display the details of a Manager.

Perimeter and Area of a Rectangle

  • Write an algorithm and flowchart to calculate the perimeter and area of a rectangle, given its length and width.

Tokens in C

  • Explain the following tokens: identifiers, keywords, constants, and character set.

Palindrome Check

  • Write a program to check if a given string is a palindrome using standard string library functions.

String Handling Functions

  • Explain string handling functions with examples.

Call by Value and Call by Reference

  • Explain call by value and call by reference mechanisms with examples.

Array Operations

  • Write a program to calculate the sum and average of array elements.
  • Explain data input and output functions used in C.

Algorithm vs. Flowchart

  • Distinguish between algorithm and flowchart.

Function Definition

  • Define a function and explain its need in C.
  • Write a program using a function to check if a number is prime.

Calculator Program

  • Design a calculator program with a switch statement for the following operations:
    • Add two numbers
    • Subtract two numbers
    • Multiply two numbers
    • Divide two numbers

Smallest Number

  • Write a program to find the smallest among three numbers using logical operators.

Matrix Multiplication

  • Write a program to perform matrix multiplication.

Leap Year Check

  • Write a program to determine if a given year is a leap year.

Function Arguments

  • Explain and demonstrate two ways of passing arguments in a function.

Factorial Calculation

  • Write a program to calculate the factorial of a number using a function.

Data Types

  • Explain the classification of data types in C with associated examples.

Operators

  • Explain operators and provide examples.

Control Statements

  • Explain the continue and break statements.
  • Describe branching control statements/constructs in C.

Pointer Operations

  • Write a program to swap two numbers using pointers.

Pattern Printing

  • Write a program to print the following patterns:
    • 1
    • 12
    • 123
    • 1234
    • 12345

Pointer Concepts

  • Explain pointers, pointer to pointers, and dynamic memory allocation functions with appropriate syntax.
  • Explain how to check if a number is zero, positive, or negative with conditionals.
  • Provide a pointer example.
  • Provide a function that checks if a number is prime.

Loops

  • Differentiate between entry-controlled and exit-controlled loops (while and do-while).

Structures and Unions

  • Explain the concepts of structures and unions.
  • Write a program to calculate the Fibonacci series using recursion.

Multi-Dimensional Arrays

  • Define multi-dimensional arrays with syntax and examples.
  • Write a program to perform matrix transpose.

Switch Statement

  • Explain the purpose of a switch statement.
  • Write a menu-driven program to perform various arithmetic operations based on user input.

Arrays

  • Explain one-dimensional and multi-dimensional arrays in detail.
  • Provide appropriate examples.

Recursion

  • Explain recursion.
  • Provide an example program to find the factorial of a number using recursion.

Defining a Structure

  • Declare a structure named “Book” to store the details of a book (title, author, price).

Book Management Program

  • Write a C program to input book details for three books.
  • Calculate the most expensive and least expensive books.
  • Display their details.

Studying That Suits You

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

Quiz Team

Related Documents

SP Question Bank PDF

Description

Test your knowledge on fundamental programming concepts including low-level vs. high-level languages, string input/output, nested structures, and more. This quiz covers essential topics such as perimeter and area calculations, palindrome checks, and string handling functions.

More Like This

C Programming Fundamentals Quiz
5 questions
Basic Web Development Concepts Quiz
4 questions
C Language Basics Quiz
5 questions
Basics of C Programming
13 questions

Basics of C Programming

LyricalFluxus1520 avatar
LyricalFluxus1520
Use Quizgecko on...
Browser
Browser