Friday, February 9, 2024

C File Management MCQs Set 2

C File Management MCQs Set 2

1. What is the correct way to check if a file was opened successfully after using fopen()?
(a) if (fopen(file_name, "mode") == NULL) { ... }
(b) if (file_pointer != NULL) { ... }
(c) if (errno == 0) { ... }
(d) None of the above

2. Which function closes a file in C?
(a) fclose(file_name)
(b) close(file_pointer)
(c) close(file_name)
(d) Both (a) and (b)

3. Which function reads a single character from a file?
(a) getchar()
(b) getc(file_pointer)
(c) fscanf(file_pointer, "%c")
(d) All of the above

4. How can you determine the current position (read/write pointer) within a file?
(a) ftell(file_pointer)
(b) fseek(file_pointer, 0, SEEK_CUR)
(c) fgetc(file_pointer)
(d) Both (a) and (b)

5. Which function closes an open file?
(a) close(file)
(b) fclose(file)
(c) terminate(file)
(d) destroy(file)

6. What does the EOF macro represent?
(a) End of File
(b) End of Stream
(c) End of Text
(d) End of Function

7. How do you check if a file has been opened successfully?
(a) if (file != NULL)
(b) if (file != EOF)
(c) if (file > 0)
(d) if (file < 0)

>>> Here is your answers


C File Management MCQs Set 2 - Answers

C File Management MCQs Set 2 - Answers

1. What is the correct way to check if a file was opened successfully after using fopen()?
(a) if (fopen(file_name, "mode") == NULL) { ... }
(b) if (file_pointer != NULL) { ... }
(c) if (errno == 0) { ... }
(d) None of the above

Answer: (b) if (file_pointer != NULL) { ... }

Explanation:
fopen() returns a FILE pointer, which is NULL on failure.

2. Which function closes a file in C?
(a) fclose(file_name)
(b) close(file_pointer)
(c) close(file_name)
(d) Both (a) and (b)

Answer: (d) Both (a) and (b)

Explanation:
Both fclose(file_pointer) and close(file_pointer) are valid.

3. Which function reads a single character from a file?
(a) getchar()
(b) getc(file_pointer)
(c) fscanf(file_pointer, "%c")
(d) All of the above

Answer: (b) getc(file_pointer)

Explanation:
getchar() reads from standard input, not files.
fscanf() is usually used for formatted input.

4. How can you determine the current position (read/write pointer) within a file?
(a) ftell(file_pointer)
(b) fseek(file_pointer, 0, SEEK_CUR)
(c) fgetc(file_pointer)
(d) Both (a) and (b)

Answer: (d) Both (a) and (b)

Explanation:
ftell(file_pointer) returns the current file position.
fseek(file_pointer, 0, SEEK_CUR) also retrieves the current position.

5. Which function closes an open file?
(a) close(file)
(b) fclose(file)
(c) terminate(file)
(d) destroy(file)

Answer: (b) fclose(file)

Explanation:
fclose(file) is the standard C library function to close an open file.
Failing to close a file can lead to memory leaks and data corruption.

6. What does the EOF macro represent?
(a) End of File
(b) End of Stream
(c) End of Text
(d) End of Function

Answer: (a) End of File

Explanation:
EOF is a macro defined in the stdio.h header, indicating the end of a file reached during reading.

7. How do you check if a file has been opened successfully?
(a) if (file != NULL)
(b) if (file != EOF)
(c) if (file > 0)
(d) if (file < 0)

Answer: (a) if (file != NULL). It is the correct way to check for successful file opening.

Explanation:
fopen() returns a pointer to the opened file stream. If the opening fails, it returns NULL.


Saturday, January 6, 2024

Flowcharts, Algorithms, and Pseudocode Key Differences

Differences between Flowcharts, Algorithms, and Pseudocode

Flowchart

  • Visual representation: Uses symbols and connecting lines to depict the steps and flow of a process or algorithm.
  • Easy to understand: Visual nature makes it accessible even for those without programming knowledge.
  • Good for communication: Effective for explaining processes to non-technical audiences and collaborating within teams.
  • Can be cumbersome for complex algorithms: May become unwieldy for large or intricate logic.


Algorithm
  • Step-by-step procedure: A precise, ordered set of instructions to solve a problem or achieve a task.
  • Foundation for problem-solving: Forms the basis for both flowcharts and pseudocode.
  • Can be expressed in various ways: Can be written in natural language, mathematical notation, or a combination of both.
  • Not necessarily executable: May require further translation into a programming language for implementation.

Pseudocode
  • Structured English-like language: Uses a combination of natural language and programming constructs to describe algorithms.
  • Close to actual code: Resembles real programming languages, making it easier to translate into code.
  • Good for detailed planning: Helps refine algorithms before coding, catching potential errors early.
  • Not standardized: Lacks strict syntax rules, allowing flexibility but potentially leading to inconsistencies.

Key Differences Summary



Best Practices
  • Use flowcharts to visualize the overall flow of a process, especially for communication and collaboration.
  • Use algorithms to clearly define the problem-solving steps in a logical and concise manner.
  • Use pseudocode to refine algorithms and make them more concrete before actual coding, ensuring clarity and accuracy.
Consider using a combination of these tools for optimal results, depending on the specific task and audience.


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.


11. If n1=5, and n2=2, then, what will be the value of expression: (n1 / n2 + n1 % n2) ?

a) 3

b) 2

c) 5

d) 7 


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.

11 a) 3


< Back to Question Bank >

< Home >