Pages

Sunday, December 31, 2023

C Branching Statements MCQs with Answer - Set 2

C - Branching Statements MCQs Set 2


Followings are 10 important C programming MCQs on branching statements, along with their answers:

1. Which of the following is not a branching statement in C?
a) if 
b) switch 
c) while 
d) goto

Answer: c) while (while is a looping statement, not a branching statement)

2. What is the output of the following code?

int x = 10;
if (x > 5) {
  printf("x is greater than 5");
} else {
  printf("x is less than or equal to 5");
}

a) x is greater than 5 
b) x is less than or equal to 5 
c) Compile error 
d) Runtime error

Answer: a) x is greater than 5

3. Which of the following is a multiway decision statement in C?
a) if 
b) switch 
c) for 
d) while

Answer: b) switch

4. Is the default case compulsory in the switch case statement?
a) Yes 
b) No 
c) Depends on the compiler
d) None of the above

Answer: b) No

5. What happens when a break statement is encountered in a switch case?
a) The program terminates
b) The switch statement terminates 
c) The current loop terminates
d) None of the above

Answer: b) The switch statement terminates

6. If the Boolean expression of an if statement evaluates to true, then which block of code is executed?
a) The block inside the if statement 
b) The block inside the else statement 
c) Both blocks 
d) None of the blocks

Answer: a) The block inside the if statement

7. Can a switch statement have multiple case labels with the same value?
a) Yes 
b) No 
c) Depends on the compiler 
d) None of the above

Answer: a) Yes

8. What is the output of the following code?

int num = 4;
switch (num + 2) {
  case 4: printf("4"); break;
  case 3: printf("3"); break;
  case 6: printf("6"); break;
}

a) 4 
b) 3 
c) 6 
d) Compile error

Answer: c) 6

9. Can a break statement be used inside an if statement?
a) Yes 
b) No 
c) Depends on the compiler 
d) None of the above

Answer: b) No

10. What is the purpose of the default case in a switch statement?
a) To handle all cases that are not explicitly handled by other cases 
b) To terminate the switch statement
c) To cause a compile error 
d) None of the above

Answer: a) To handle all cases that are not explicitly handled by other cases

< HOME

Saturday, December 30, 2023

Top 10 C Programming Libraries

Top 10 C Programming Libraries

Ranking C libraries can be very subjective, and dependent on your own usage. However, here are 10 C libraries that are widely used across various domains:

1. Standard C Library (libc):

  • Foundational library included with every C compiler.
  • Provides essential functions for input/output, memory management, string manipulation, math operations, and more.
  • Basis for most C programming.

2. GLib:

  • General-purpose utility library used in many C projects, including GNOME desktop applications.
  • Offers data structures, functions for string handling, networking, file I/O, threading, and more.
  • Cross-platform and supports various programming paradigms.

3. OpenSSL:

  • Toolkit for implementing secure communication protocols like TLS and SSL.
  • Provides cryptographic functions for encryption, decryption, authentication, and certificate management.
  • Essential for building secure network applications.

4. libcurl:

  • Client-side URL transfer library for fetching data from web servers.
  • Supports various protocols (HTTP, HTTPS, FTP, etc.), authentication methods, and proxy settings.
  • Widely used in web applications and command-line tools.

5. libpng:

  • Reference library for reading and writing PNG image files.
  • Handles PNG compression, decompression, color management, and metadata.
  • Widely used for working with PNG images in C programs.

6. libjpeg:

  • Library for handling JPEG image files, similar to libpng for PNG.
  • Compresses and decompresses JPEG images, supports various color spaces and features.
  • Essential for working with JPEG images in C.

7. zlib:

  • General-purpose data compression library, often used with other libraries.
  • Implements the DEFLATE compression algorithm, commonly used in PNG, gzip, and other formats.
  • Reduces file sizes and network transfer times.

8. SQLite:

  • Embedded SQL database engine, allowing C programs to store and manage data without an external database server.
  • Self-contained, serverless, and lightweight, making it suitable for embedded systems and mobile apps.

9. SDL (Simple DirectMedia Layer):

  • Cross-platform library for multimedia programming, handling input, audio, graphics, and more.
  • Widely used for developing games, emulators, and media applications in C.

10. OpenGL:

  • Graphics API for rendering 2D and 3D graphics, often used with SDL for cross-platform compatibility.
  • Provides hardware-accelerated rendering for high-performance graphics applications.


Friday, October 13, 2023

Flowchart to check prime number

Prime Number

" a whole number greater than 1 that cannot be exactly divided by any whole number other than itself and 1 (e.g. 2, 3, 5, 7, 11)."

Prime numbers are very useful in cryptography.

Flowchart to check a Prime Number

Following flowchart is prepared using a RAPTOR software.
Prime number Flowchart

Python Logic to check a prime number.

def is_prime(n):
  if n <= 1:
    return False
  for i in range(2, int(n**0.5) + 1):
    if n % i == 0:
      return False
  return True

Output of python program:

is_prime(5)
True



Friday, September 8, 2023

C Operators MCQs Set 2

C Operators MCQs Set 2

Important MCQs based on C Programming Operators. 


1. What does the % operator do in C?

a) Addition

b) Subtraction

c) Multiplication

d) Modulus


2. Which operator is used for logical NOT in C?

a) &

b) &&

c) !

d) ||


3. What is the result of the expression 5 + 3 * 2 in C?

a) 10

b) 16

c) 11

d) 13


4. What is the result of the expression 10 / 3 in C?

a) 3.3333

b) 3

c) 3.0

d) 4


5. Which operator is used to check if two values are equal in C?

a) ==

b) =

c) !=

d) ===


6. What does the ++ operator do when placed before a variable, like ++x?

a) Decrements the variable by 1

b) Multiplies the variable by 2

c) Increments the variable by 1

d) Doubles the value of the variable


7. Which operator is used to access the value stored at a memory address in C?

a) &

b) *

c) ->

d) .


8. What does the += operator do in C?

a) Subtracts the right-hand value from the left-hand value

b) Multiplies the left-hand value by the right-hand value

c) Adds the right-hand value to the left-hand value

d) Divides the left-hand value by the right-hand value


9. Which operator is used to shift the bits of a number to the left in C?

a) >>

b) <<

c) &

d) |


10. What does the ternary conditional operator ? : do in C?

a) It checks if a condition is true and returns 1 if true, 0 if false.

b) It assigns a value to a variable.

c) It performs addition.

d) It provides a shorthand way to write an if-else statement.


Correct Answers:


1. d) Modulus

2. c) !

3. c) 11

4. b) 3

5. a) ==

6. c) Increments the variable by 1

7. b) *

8. c) Adds the right-hand value to the left-hand value

9. b) <<

10 d) It provides a shorthand way to write an if-else statement.


< Back to Question Bank >

< Home >


Monday, June 12, 2023

Top 10 C Programming Topics for Beginners

Followings are the top 10 topics for the C programming beginners. These topics are essential for understanding the C programming language and for writing effective C programs:

Top 10 Topics for C Programming Beginners

  1. Data types
  2. Variables
  3. Input / Output (I/O) Operations
  4. Operators
  5. Control flow statements
  6. Functions
  7. Arrays
  8. Strings
  9. Pointers
  10. File handling

Here is a brief overview of each topic:

  • Data types: Data types are the building blocks of C programs. They define the type of data that can be stored in a variable.
  • Variables: Variables are used to store data. They are given names and can be used to store values of different data types.
  • Input / Output (I/O) Operations: I/O operations are used to manage data input from the user or a File. printf() and scanf() are two popular I/O functions.
  • Operators: Operators are used to perform operations on data. They can be used to perform arithmetic operations, logical operations, and string operations.
  • Control flow statements: Control flow statements are used to control the flow of execution of a C program. They can be used to make decisions, loops, and jumps.
  • Functions: Functions are blocks of code that are reusable. They can be called from other parts of a C program.
  • Arrays: Arrays are variables that can store multiple values of the same data type.
  • Strings: Strings are arrays of characters. They are used to store text data.
  • Pointers: Pointers are variables that store the address of another variable. They can be used to access the value of another variable indirectly.
  • File handling: File handling is used to read and write data to files. It is a powerful tool that can be used to store and retrieve data from files.

These are just a few of the many topics that every C programming beginners should learn. If you are interested in learning C programming, we recommend that you find a good book or online Self Study Tutorials that covers these topics in detail.




Tuesday, April 18, 2023

C Programming Predict Output Strings MCQs Set 3 - Answer

Predict Output MCQs Based On C Programming Strings - Answer

1. What is the output of the following C program?

#include <stdio.h>

int main() {
char str1[] = "hello";
char str2[] = "world";
printf("%s", str1);
printf("%s", str2);
return 0;
}

A) helloworld
B) hello world
C) hello
   world
D) Compilation error

Answer: A) helloworld

Explanation: str1 array contains the string "hello", and str2 contains the string "world". The printf function outputs str1 followed by str2, which results in "helloworld".

2. What is the output of the following C program?

#include <stdio.h>
int main() {
char str[] = "hello";
printf("%c", str[3]);
return 0;
}

A) h
B) e
C) l
D) o

Answer: C) l

Explanation: The str array contains the string "hello". The expression str[3] retrieves the character at index 3 of the array, which is 'l'. Hence program outputs 'l'.

3. What is the output of the following C program?

#include <stdio.h>
int main() {
char str1[] = "hello";
char str2[] = "world";
printf("%s", str1 + 3);
printf("%s", str2 + 1);
return 0;
}
A) loorld
B) loorldw
C) loworld
D) helloworld

Answer: A) loorld

Explanation: The expression str1 + 3 evaluates to a pointer to the fourth character of str1, which is 'l'. The printf function then outputs the remaining characters of str1 starting from the fourth character, which is "lo". Similarly, the expression str2 + 1 evaluates to a pointer to the second character of str2, which is 'o'. The printf function then outputs the remaining characters of str2 starting from the second character, which is "orld". Hence, final output will be "loorld".

4. What is the output of the following C program?

#include <stdio.h>
int main() {
char str[] = "hello";
str[2] = '\0';
printf("%s", str);
return 0;
}
A) hel
B) he
C) hello
D) Compilation error

Answer: B) he

Explanation: The str contains the string "hello". The statement str[2] = '\0' sets the third character of str to the null character, which terminates the string. The printf function then outputs the characters of str up to the null character, which is "he".

5. What is the output of the following C program?

#include <stdio.h>
int main() {
int arr[] = {2, 3, 4};
int *p = arr;
printf("%d", *(p + 1));
return 0;
}
A) 2
B) 3
C) 4
D) Compilation error

Answer: B) 3

Explanation: The pointer p points to the first element of the array arr. The expression *(p + 1) adds 1 to the pointer p to point to the second element of the array, and then evaluates to the value pointed to by the new pointer, which is 3.




C Programming String Predict Output MCQs Set 3

 Predict output MCQs based on C Programming Strings

1. What is the output of the following C program?

#include <stdio.h>

int main() {

   char str1[] = "hello";

   char str2[] = "world";

   printf("%s", str1);

   printf("%s", str2);

   return 0;

}

A) helloworld

B) hello world

C) hello

   world

D) Compilation error


2. What is the output of the following C program?

#include <stdio.h>

int main() {

   char str[] = "hello";

   printf("%c", str[3]);

   return 0;

}

A) h

B) e

C) l

D) o


3. What is the output of the following C program?

#include <stdio.h>

int main() {

   char str1[] = "hello";

   char str2[] = "world";

   printf("%s", str1 + 3);

   printf("%s", str2 + 1);

   return 0;

}

A) loorld

B) loorldw

C) loworld

D) helloworld


4. What is the output of the following C program?

#include <stdio.h>

int main() {

   char str[] = "hello";

   str[2] = '\0';

   printf("%s", str);

   return 0;

}

A) hel

B) he

C) hello

D) Compilation error


5. What is the output of the following C program?

#include <stdio.h>

int main() {

   int arr[] = {2, 3, 4};

   int *p = arr;

   printf("%d", *(p + 1));

   return 0;

}

A) 2

B) 3

C) 4

D) Compilation error


Check ANSEWR here...



Wednesday, March 15, 2023

C program to decrypt a text file using Caesar Cipher

Caesar Cipher File Decryptor


This C program will decrypt a given text file into plain text file using Caesar Cipher cryptographic algorithm.


#include <stdio.h>
#define MAX_FILE_SIZE 1000

//function to decrypt message based on given key
void decrypt(char *message, int key) {
    char ch;
    int i;
    for(i = 0; message[i] != '\0'; ++i){
        ch = message[i];
        if(ch >= 'a' && ch <= 'z'){
            ch = ch - key;
            if(ch < 'a'){
                ch = ch + 'z' - 'a' + 1;
            }
            message[i] = ch;
        }
        else if(ch >= 'A' && ch <= 'Z'){
            ch = ch - key;
            if(ch < 'A'){
                ch = ch + 'Z' - 'A' + 1;
            }
            message[i] = ch;
        }
    }
}

int main() {
    FILE *fileIn, *fileOut;
    char message[MAX_FILE_SIZE];
    int key;

    // Open the encrypted file
    fileIn = fopen("demo1.txt", "r");
    if(fileIn == NULL){
        printf("File open error.\n");
        return 0;
    }

    // Read the encrypted message from given file
    fgets(message, MAX_FILE_SIZE, fileIn);

    // Close the input file
    fclose(fileIn);

    // Prompt user to enter the key
    printf("Enter key: ");
    scanf("%d", &key);

    // Decrypt the message
    decrypt(message, key);

    // Open the output file
    fileOut = fopen("decrypted.txt", "w");
    if(fileOut == NULL){
        printf("File write error.\n");
        return 0;
    }

    // Write the decrypted message to output file
    fprintf(fileOut, "%s", message);

    // Close the output file
    fclose(fileOut);

    printf("File decrypted successfully.\n");

    return 0;
}

Thursday, February 23, 2023

C File Management Important MCQs

C Programming File Management Important MCQs


1. Which function is used to open a file in C programming language?

(a) fopen()

(b) open()

(c) read()

(d) close()


2. What is the correct syntax of the fopen() function?

(a) fopen("filename", "mode");

(b) fopen("mode", "filename");

(c) fopen("filename", "access");

(d) fopen("access", "filename");


3. Which mode is used to open a file for reading in C programming language?

(a) r

(b) w

(c) a

(d) x


4. Which mode is used to open a file for writing in C programming language?

(a) r

(b) w

(c) a

(d) x


5. What is the syntax of the fclose() function?

(a) fclose();

(b) fclose(file);

(c) fclose("filename");

(d) fclose(filename);


6. Find the purpose of the fseek() function in C File Management.

(a) To move the file pointer to a specific position in the file

(b) To close a file

(c) To read a file

(d) To write to a file


7. What is the purpose of the remove() function in C File Management?

(a) To rename a file

(b) To delete a file

(c) To create a new file

(d) To close a file


8. What is the syntax for opening a file in C programming?

(a) FILE *fp = fopen("filename", "mode");

(b) file fp = fopen("filename", "mode");

(c) FILE *fp = fopen("mode", "filename");

(d) none of these

< Check your ANSWER Here >


File Management MCQs with Answers

C Programming File Management Important MCQs with Answers

1. Which function is used to open a file in C programming language?

(a) fopen()

(b) open()

(c) read()

(d) close()

Answer: (a) fopen()


2. What is the correct syntax of the fopen() function?

(a) fopen("filename", "mode");

(b) fopen("mode", "filename");

(c) fopen("filename", "access");

(d) fopen("access", "filename");

Answer: (a) fopen("filename", "mode");


3. Which mode is used to open a file for reading in C programming language?

(a) r

(b) w

(c) a

(d) x

Answer: (a) r


4. Which mode is used to open a file for writing in C programming language?

(a) r

(b) w

(c) a

(d) x

Answer: (b) w


5. What is the syntax of the fclose() function?

(a) fclose();

(b) fclose(file);

(c) fclose("filename");

(d) fclose(filename);

Answer: (b) fclose(file);


6. Find the purpose of the fseek() function in C File Management.

(a) To move the file pointer to a specific position in the file

(b) To close a file

(c) To read a file

(d) To write to a file

Answer: (a) To move the file pointer to a specific position in the file


7. What is the purpose of the remove() function in C File Management?

(a) To rename a file

(b) To delete a file

(c) To create a new file

(d) To close a file

Answer: (b) To delete a file


8. What is the syntax for opening a file in C programming?

(a) FILE *fp = fopen("filename", "mode");

(b) file fp = fopen("filename", "mode");

(c) FILE *fp = fopen("mode", "filename");

(d) none of these

Answer: (a) FILE *fp = fopen("filename", "mode");



Wednesday, February 15, 2023

Comparison between Python, C and Java

This article provides comparison between three most popular computer programming languages: Python, C and Java.

Python, C and Java


Python, C, and Java are three different programming languages. Each language has its own strengths and weaknesses. Following is a short comparison of these three computer programming languages:

Python


Python is a high-level, interpreted language that is known for its simplicity, readability, and versatility. It has a vast standard library, and it is often used for scripting, web development, data analysis, machine learning, and artificial intelligence. It is dynamically typed, which means that variables don't need to be explicitly declared, and it has a simple syntax that makes it easy to learn and use.

  • Pros: Easy to learn, concise and readable syntax, large standard library, dynamic typing, strong support for scientific computing and data analysis, good for scripting and automation tasks.
  • Cons: Slower compared to other compiled languages, not the best option for low-level programming, doesn't offer as much control over memory management, and not ideal for developing large-scale applications.

C Language

C is a low-level language that is used for system programming, embedded systems, and other tasks that require direct access to hardware. It is a compiled language, which means that it is translated into machine code by a compiler before being executed. C is known for its efficiency, control over memory management, and low-level access to system resources.

  • Pros: Fast and efficient, direct access to system resources, control over memory management, widely used in system programming and embedded systems.
  • Cons: Complex syntax, difficult to learn, no built-in support for object-oriented programming, no automatic garbage collection, prone to memory leaks and buffer overflows.

Java


Java is a high-level language that is known for its portability, security, and object-oriented programming features. It is compiled into bytecode, which is then interpreted by the Java Virtual Machine (JVM). Java is used for a wide range of applications, including web development, mobile app development, and enterprise software.

  • Pros: Platform independence, strong support for object-oriented programming, automatic memory management, rich standard library, good for developing large-scale applications.
  • Cons: Slower compared to other compiled languages, requires the JVM to be installed on the system, more verbose syntax than Python, and requires more memory compared to other languages.

Summary:

In summary, each language has its own strengths and weaknesses, and the choice of language depends on the specific requirements of the project. Python is often used for scripting and data analysis, C is used for system programming and embedded systems, and Java is used for enterprise software and mobile app development.