0% found this document useful (0 votes)
249 views9 pages

Linux Bash Shell Programming MCQs - Sanfoundry

This document contains 10 multiple choice questions about variables in Linux shell programming. It covers topics like what types of variables shells use, how to assign values to variables, how to extract variable values, and variable scope. The questions are followed by explanations of the correct answers.

Uploaded by

Samiksha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
249 views9 pages

Linux Bash Shell Programming MCQs - Sanfoundry

This document contains 10 multiple choice questions about variables in Linux shell programming. It covers topics like what types of variables shells use, how to assign values to variables, how to extract variable values, and variable scope. The questions are followed by explanations of the correct answers.

Uploaded by

Samiksha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

1/31/24, 4:35 AM Linux Bash Shell Programming MCQs - Sanfoundry

Linux Shell Programming Questions and


Answers – Variables
This set of Linux / Unix questions and answers focuses on variables in Linux Shell Programming.

1. In the shell, by default, all variables are considered and stored as


a) string
b) integer
c) character
d) float
View Answer

Answer: a
Explanation: None.

2. Which command reads user input from the terminal and assign this value to a variable name?
a) read
b) get
c) declare
d) set
View Answer

Answer: a
Explanation: None.

3. Which one of the following statement is true about variables in shell?


a) variables do not require declaration before assigning value to them
b) variables are case sensitive
c) to extract the contents of a variable, we have to provide the variable a preceding $
d) all of the mentioned
View Answer

Answer: d
Découvertes santé
Explanation: None.
Anti-âge : erreur n°1 à éviter
Ne faites plus l'erreur N°1 que font TOUTES les femmes de +60 ans avec leur peau.
https://www.sanfoundry.com/linux-shell-programming-questions-answers-variables/ 1/9
1/31/24, 4:35 AM Linux Bash Shell Programming MCQs - Sanfoundry

4. Which one of the following is not a valid shell variable?


a) _san
b) san_2
c) _san_2
d) 2_san
View Answer

Answer: d
Explanation: The shell variable can contain only letters(a to z or A to Z), numbers(0 to 9), or a
underscore character(_) and a variable can not start with a number.

advertisement

5. To redefine a variable, it can be removed from the list of variables by using the command
a) unset
b) delete
c) remove
d) clear
View Answer

Answer: a
Explanation: None.

6. What is the output of this program?

Note: Join free Sanfoundry classes at Telegram or Youtube

https://www.sanfoundry.com/linux-shell-programming-questions-answers-variables/ 2/9
1/31/24, 4:35 AM Linux Bash Shell Programming MCQs - Sanfoundry

1. #!/bin/bash
2. san_var="Sanfoundry"
3. echo "$san_var"
4. echo '$san_var'
5. echo '"$san_var"'
6. echo "'$san_var'"
7. echo \$san_var
8. exit 0

a) Sanfoundry
$san_var
“$san_var”
‘Sanfoundry’
$san_var
b) Sanfoundry
Sanfoundry
“Sanfoundry”
‘Sanfoundry’
Sanfoundry
c) program will generate an error message
d) program will print nothing
View Answer

Answer: a
Explanation: Using double quotes does not affect the substitution of the variable, while single
quotes and backslash do.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
Sanfoundry
$san_var
“$san_var”
‘Sanfoundry’
$san_var
root@ubuntu:/home/sanfoundry#

advertisement

https://www.sanfoundry.com/linux-shell-programming-questions-answers-variables/ 3/9
1/31/24, 4:35 AM Linux Bash Shell Programming MCQs - Sanfoundry

7. What is the output of this program?

1. #!/bin/bash
2. var1=10
3. $var1=20
4. echo $var1
5. exit 0

a) program will print 10


b) program will generate a warning message
c) program will print 20
d) program will print 10 & 20
View Answer

Answer: d
Explanation: The doller sign ($) is used to access a variable’s value, not to define it.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
./test.sh: line 3: 10=20: command not found
10
root@ubuntu:/home/sanfoundry#

advertisement

8. What is the output of this program?

https://www.sanfoundry.com/linux-shell-programming-questions-answers-variables/ 4/9
1/31/24, 4:35 AM Linux Bash Shell Programming MCQs - Sanfoundry

1. #!/bin/bash
2. var[1]=san_1
3. var[2]=san_2
4. var[3]=san_3
5. echo ${var[*]}
6. exit 0

a) san_1
b) san_2
c) san_3
d) san_1 san_2 san_3
View Answer

Answer: d
Explanation: All items of an array can be accessed by using ${[*]} or ${[@]}.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
san_1 san_2 san_3
root@ubuntu:/home/sanfoundry#

9. What is the output of this program?

1. #!/bin/bash
2. san_var=hello
3. readonly san_var
4. san_var=hi
5. echo $san_var
6. exit 0

a) hello
b) hi
c) nothing will print
d) none of the mentioned
View Answer

Answer: a
Explanation: After the execution of the ‘readonly’ command, shell will not provide the permission
to overwrite the value stored in variable ‘san_var’.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
./test.sh: line 4: san_var: readonly variable
hello
root@ubuntu:/home/sanfoundry#

10. What is the output of this program?

https://www.sanfoundry.com/linux-shell-programming-questions-answers-variables/ 5/9
1/31/24, 4:35 AM Linux Bash Shell Programming MCQs - Sanfoundry

1. #!/bin/bash
2. san_var=10
3. echo "the value of \"san_var\" is $san_var"
4. exit 0

a) the value of “san_var” is 10


b) the value of is 10
c) the value of san_var is $san_var
d) the value of “san_var” is $san_var
View Answer

Answer: a
Explanation: None.
Output:
root@ubuntu:/home/sanfoundry# ./test.sh
the value of “san_var” is 10
root@ubuntu:/home/sanfoundry#

Sanfoundry Global Education & Learning Series – Linux Administration & Programming.

Here’s the list of Best Books in Linux Commands & Shell Programming.
Here’s the list of Best Books in Linux Kernel, Device-Drivers & System Programming.

To practice all questions on Linux Administration & Programming, here is complete set of 1000+
Multiple Choice Questions and Answers on Linux.

« Prev - Linux Shell Programming Questions » Next - Linux Bash Shell Questions & Answers –
and Answers – Functions Arithmetic Expressions

Related Posts:

Practice Programming MCQs


Apply for Programming Internship
Check Information Technology Books
Check Linux Books

advertisement

https://www.sanfoundry.com/linux-shell-programming-questions-answers-variables/ 6/9
1/31/24, 4:35 AM Linux Bash Shell Programming MCQs - Sanfoundry

Recommended Articles:
1. Linux Shell Programming Questions and Answers – Functions
2. Linux Questions & Answers – Shell Special Symbols
3. Linux Questions & Answers – Shell Environment – 1
4. Linux Questions & Answers – Shell Programming
5. Linux Questions & Answers – Shell Environment – 2
6. Unix Questions and Answers – Command Substitution and Shell Variables
7. Unix Questions and Answers – Shell Variables – 1
8. Linux Bash Shell Questions & Answers – Built-in Commands – 1
9. Awk Programming Questions & Answers – Variables & Array
10. Linux Bash Shell Questions & Answers – Arithmetic Expressions

advertisement

Additional Resources:
Linux MCQ Questions
https://www.sanfoundry.com/linux-shell-programming-questions-answers-variables/ 7/9
1/31/24, 4:35 AM Linux Bash Shell Programming MCQs - Sanfoundry

C Programming Interview Questions


C Programming MCQ Questions
Linux Commands with Examples
C Programming Examples

Popular Pages:
Advanced C Programming
C++ Programming MCQ Questions
Java Programming MCQ Questions
Java Programming Examples
Object Oriented Programming MCQ Questions

Subscribe: Linux Newsletter

Name

Email

Subscribe

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to


get free Certificate of Merit. Join our social networks below and stay updated with latest contests,
videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is
Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on
development of Linux Kernel, SAN Technologies, Advanced C, Data
Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram


SanfoundryClasses.

https://www.sanfoundry.com/linux-shell-programming-questions-answers-variables/ 8/9
1/31/24, 4:35 AM Linux Bash Shell Programming MCQs - Sanfoundry

About | Certifications | Internships | Jobs | Privacy Policy | Terms | Copyright | Contact

     

© 2011-2024 Sanfoundry. All Rights Reserved.

https://www.sanfoundry.com/linux-shell-programming-questions-answers-variables/ 9/9

You might also like