MCQs on Basic Input and Output | Perl

Here are 30 multiple-choice questions (MCQs) based on Chapter 4: Basic Input and Output, covering topics such as reading and writing to files, standard input/output operations, and formatting output with printf and sprintf. These MCQs are designed to test your understanding of file handling and output formatting in Perl.


Reading and Writing to Files

  1. Which of the following file modes opens a file for reading only in Perl?
    • A) ‘w’
    • B) ‘r’
    • C) ‘rw’
    • D) ‘a’
  2. What is the correct way to open a file for writing in Perl?
    • A) open($file, ‘r’, ‘file.txt’)
    • B) open($file, ‘w’, ‘file.txt’)
    • C) open($file, ‘a’, ‘file.txt’)
    • D) open($file, ‘rw’, ‘file.txt’)
  3. Which function is used to read from a file in Perl?
    • A) write()
    • B) read()
    • C) print()
    • D) <$file>
  4. Which function is used to close a file after performing operations in Perl?
    • A) close()
    • B) finish()
    • C) end()
    • D) closefile()
  5. How can you check for errors while opening a file in Perl?
    • A) By checking the $! variable
    • B) By using the check() function
    • C) By testing the file descriptor
    • D) By using opencheck()
  6. What does the ‘a’ mode in the open() function do in Perl?
    • A) Opens a file for reading
    • B) Opens a file for writing and appending
    • C) Opens a file for reading and writing
    • D) None of the above
  7. Which of the following is the correct way to read an entire file into a string in Perl?
    • A) $data = <FILE>;
    • B) $data = read(FILE);
    • C) $data = print(FILE);
    • D) $data = <$file>;
  8. What is the correct syntax to write to a file in Perl?
    • A) print FILE “Hello World\n”;
    • B) write FILE “Hello World\n”;
    • C) print “Hello World\n” to FILE;
    • D) write “Hello World\n” to FILE;
  9. How can you open a file for both reading and writing in Perl?
    • A) open($file, ‘r+’)
    • B) open($file, ‘w+’)
    • C) open($file, ‘rw’)
    • D) open($file, ‘a+’)
  10. What is the output when trying to open a non-existent file in Perl using the ‘r’ mode?
  • A) The program creates the file
  • B) The program throws an error
  • C) The program prints an error message
  • D) The program does nothing

Standard Input and Output Operations

  1. Which function in Perl is used to print output to the screen?
  • A) print()
  • B) output()
  • C) display()
  • D) write()
  1. Which function is used to read input from the user in Perl?
  • A) input()
  • B) readline()
  • C) gets()
  • D) <STDIN>
  1. Which of the following is the correct syntax to read a line of input from the user in Perl?
  • A) $input = <STDIN>;
  • B) $input = get(STDIN);
  • C) $input = readline(STDIN);
  • D) $input = input();
  1. How can you print output without adding a new line at the end in Perl?
  • A) print “Hello”;
  • B) print “Hello”, no_newline;
  • C) print “Hello”, “\n”;
  • D) print “Hello”, “\0”;
  1. What does the chomp() function do in Perl?
  • A) Removes leading whitespace from a string
  • B) Removes the trailing newline from a string
  • C) Adds a newline to the end of a string
  • D) Checks if a string has a newline character
  1. Which of the following prints the value of a variable \$x in Perl?
  • A) print $x;
  • B) print $x;
  • C) print “$x”;
  • D) print “$x”;
  1. What does the following Perl statement do?
    print “Enter a value: “;
    A) Prints “Enter a value: ” to the file
    B) Prints “Enter a value: ” to the screen
    C) Reads a value from the user
    D) Saves “Enter a value: ” in a variable
  1. How do you print formatted text with a specified width in Perl?
  • A) printf(“%10s”, $value);
  • B) format(“%10s”, $value);
  • C) print(“%10s”, $value);
  • D) printf($value, “%10s”);
  1. What is the function used to read an entire line of input in Perl?
  • A) <STDIN>;
  • B) readline();
  • C) gets();
  • D) input();
  1. Which of the following is the correct syntax to print a string with variables in Perl?
  • A) print(“Hello $name”);
  • B) print(‘Hello $name’);
  • C) print(Hello $name);
  • D) print(Hello + $name);

Formatting Output with printf and sprintf

  1. What is the purpose of the printf function in Perl?
  • A) It is used to print output with special formatting
  • B) It prints raw strings
  • C) It reads formatted input
  • D) It prints to a file
  1. Which of the following correctly formats a number to display two decimal places using printf?
  • A) printf(“%.2f”, $number);
  • B) printf(“%.2d”, $number);
  • C) printf(“%2f”, $number);
  • D) printf(“%2.2f”, $number);
  1. How can you assign a formatted string to a variable using sprintf in Perl?
  • A) $formatted = sprintf(“Hello %s”, $name);
  • B) $formatted = sprintf(Hello %s, $name);
  • C) $formatted = “sprintf(Hello %s, $name)”;
  • D) $formatted = “Hello %s”, $name;
  1. Which of the following Perl functions can be used to print output with specific widths for numbers?
  • A) printf()
  • B) sprintf()
  • C) both A and B
  • D) print()
  1. How would you format an integer with a width of 6 using printf?
  • A) printf(“%6d”, $num);
  • B) printf(“%d6”, $num);
  • C) printf(“6%d”, $num);
  • D) printf(“%d”, $num);
  1. What is the default separator used by print() for output in Perl?
  • A) Space
  • B) Comma
  • C) Newline
  • D) Tab
  1. How do you round a floating-point number to 2 decimal places using sprintf in Perl?
  • A) $result = sprintf(“%.2f”, $num);
  • B) $result = sprintf(“%2f”, $num);
  • C) $result = sprintf(“%.2d”, $num);
  • D) $result = sprintf(“%2.2f”, $num);
  1. Which of the following statements is true about the printf function?
  • A) It outputs formatted text to the screen
  • B) It reads input from the user
  • C) It formats text and writes it to a file
  • D) It reads formatted input
  1. How can you align text to the right in printf in Perl?
  • A) printf(“%-10s”, $text);
  • B) printf(“%10s”, $text);
  • C) printf(“%s”, $text);
  • D) printf(“%-s”, $text);
  1. Which function in Perl allows you to format strings without printing them directly?
  • A) sprintf()
  • B) printf()
  • C) print()
  • D) format()

Answer Key

QnoAnswer
1B) ‘r’
2B) open($file, ‘w’, ‘file.txt’)
3D) <$file>
4A) close()
5A) By checking the $! variable
6B) Opens a file for writing and appending
7A) $data = <FILE>;
8A) print FILE “Hello World\n”;
9A) open($file, ‘r+’)
10B) The program throws an error
11A) print()
12D) <STDIN>
13A) $input = <STDIN>;
14A) print “Hello”;
15B) Removes the trailing newline from a string
16C) print “$x”;
17B) Prints “Enter a value: ” to the screen
18A) printf(“%10s”, $value);
19A) <STDIN>;
20A) print(“Hello $name”);
21A) It is used to print output with special formatting
22A) printf(“%.2f”, $number);
23A) $formatted = sprintf(“Hello %s”, $name);
24C) both A and B
25A) printf(“%6d”, $num);
26C) Newline
27A) $result = sprintf(“%.2f”, $num);
28A) It outputs formatted text to the screen
29B) printf(“%10s”, $text);
30A) sprintf()

			

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top