Home »
MCQs
Batch Script MCQs (Multiple-Choice Questions)
Practice Batch Script MCQs with answers and explanations to test your knowledge of Windows Command Prompt, batch files, variables, commands, conditional statements, loops, parameters, file handling, and automation.
Batch Script MCQs
These Batch Script multiple-choice questions cover fundamental and practical concepts used when creating and executing .bat and .cmd files in the Windows Command Shell.
List of Batch Script MCQs
The following Batch Script MCQs include questions on commands, variables, operators, loops, conditions, batch parameters, subroutines, environment localization, redirection, and practical script behavior.
1. Which file extensions are commonly used for Windows batch files?
.bat and .cmd
.sh and .bash
.ps1 and .py
.exe and .dll
Answer: A) .bat and .cmd
Explanation:
Windows batch scripts are commonly stored with the .bat or .cmd extension and are executed by the Command Shell.
2. Which command starts a new instance of the Windows command interpreter?
shell
cmd
command
console
Answer: B) cmd
Explanation:
The cmd command starts a new instance of cmd.exe, the Windows Command Shell.
3. Which command is used to assign a value to an environment variable?
var
set
let
define
Answer: B) set
Explanation:
The set command creates or changes an environment variable. For example, set name=John assigns the value John to name.
4. How is the value of a normal batch variable referenced?
$(name)
{name}
%name%
!name!
Answer: C) %name%
Explanation:
Standard environment-variable expansion in a batch file uses percent signs, such as %name%. Exclamation-mark expansion is associated with delayed expansion.
5. Which command suppresses the display of commands while a batch file executes?
echo off
hide on
silent
quiet
Answer: A) echo off
Explanation:
echo off turns off command echoing. The commonly used form @echo off also prevents the echo off command itself from being displayed.
6. Which command displays a message in a batch script?
print
display
echo
write
Answer: C) echo
Explanation:
The echo command displays text or controls command echoing in the Command Shell.
7. Which command lists files and directories in the current directory?
ls
dir
list
show
Answer: B) dir
Explanation:
The Windows dir command displays a list of files and subdirectories.
8. Which command changes the current directory?
move
cd
goto
path
Answer: B) cd
Explanation:
The cd command, also known as chdir, changes the current directory used by the command interpreter.
9. Which command creates a directory?
md
new
createdir
folder
Answer: A) md
Explanation:
md is an alias for mkdir and creates a new directory.
10. Which command deletes a file?
remove
del
rd
erasefile
Answer: B) del
Explanation:
The del command deletes one or more files. rd or rmdir is used for directories.
11. Which command copies files from one location to another?
copy
cp
clone
duplicate
Answer: A) copy
Explanation:
The Windows copy command copies one or more files to another location.
12. Which command moves a file or directory?
mv
move
transfer
shiftfile
Answer: B) move
Explanation:
The move command moves files or directories from one location to another.
13. Which command displays the contents of a text file?
cat
type
read
showfile
Answer: B) type
Explanation:
The Windows type command displays the contents of a text file in the Command Shell.
14. Which operator redirects command output to a file and overwrites the existing contents?
>
>>
<
||
Answer: A) >
Explanation:
The > redirection operator sends standard output to a file, replacing existing contents. The >> operator appends output.
15. Which operator appends command output to an existing file?
>
>>
<<
::
Answer: B) >>
Explanation:
The >> operator redirects output by appending it to the destination file rather than replacing its existing contents.
16. Which symbol is commonly used to write a comment in a batch file?
#
//
REM
--
Answer: C) REM
Explanation:
REM is the standard batch command used to add comments. A line beginning with :: is also commonly used as a comment-like construct, but REM is the documented command.
17. Which syntax correctly checks whether a file exists?
if file test.txt
if exist test.txt
exists test.txt
check test.txt
Answer: B) if exist test.txt
Explanation:
The if exist condition tests whether the specified file or directory exists.
18. Which comparison operator means "equal to" for numeric comparisons in the Batch if command?
EQ
EQU
==
EQUAL
Answer: B) EQU
Explanation:
The Batch if command supports numeric comparison operators including EQU, NEQ, LSS, LEQ, GTR, and GEQ.
19. Which Batch Script comparison operator means "not equal" for numeric values?
NEQ
NOTEQ
!=
DIFF
Answer: A) NEQ
Explanation:
NEQ is the numeric "not equal" comparison operator supported by the Batch if command.
20. Which keyword tests whether the previous command returned an error level equal to or greater than a specified value?
status
errorlevel
returncode
exitcode
Answer: B) errorlevel
Explanation:
The if errorlevel number condition tests the exit code returned by the preceding program or command. For this form, the condition is true when the value is equal to or greater than the specified number.
21. Which command implements iteration over a set of files or values?
loop
repeat
for
iterate
Answer: C) for
Explanation:
The for command executes a command for each item in a specified set. It can process files, directories, text strings, or ranges of values.
22. Which syntax is correct for a for loop inside a batch file?
for %f in (*.txt) do echo %f
for %%f in (*.txt) do echo %%f
foreach %%f (*.txt) echo %%f
for each %%f in (*.txt)
Answer: B) for %%f in (*.txt) do echo %%f
Explanation:
Inside a batch file, a for variable is written with two percent signs, such as %%f. At the interactive command prompt, a single percent sign is used.
23. What does for /f primarily provide?
- File encryption
- Parsing text and command output
- File compression
- Process termination
Answer: B) Parsing text and command output
Explanation:
The /f option enables the for command to parse text from files, strings, or command output according to specified parsing rules.
24. Which command transfers control to a labeled location in a batch file?
jump
goto
label
move
Answer: B) goto
Explanation:
The goto command transfers command processing to a label such as :start in the batch file.
25. How is a label defined in a batch file?
label start
#start
:start
@start
Answer: C) :start
Explanation:
A batch label begins with a colon. For example, :start defines a label that can be targeted by goto start.
26. Which command can call another batch file and return to the calling script?
run
call
execute
include
Answer: B) call
Explanation:
The call command invokes another batch program or a batch subroutine. Control returns to the calling batch file after the called batch program or subroutine completes.
27. Which command ends the current batch script or batch subroutine?
stop
exit
quitbatch
terminate
Answer: B) exit
Explanation:
The exit command can terminate the command interpreter or, with /b, terminate the current batch script or subroutine.
28. What does exit /b do when used inside a batch subroutine?
- Restarts the computer
- Returns control from the batch subroutine
- Deletes the batch file
- Closes every Command Prompt window
Answer: B) Returns control from the batch subroutine
Explanation:
exit /b exits the current batch script context or subroutine. When used in a called subroutine, control can return to the calling script.
29. Which command pauses a batch script and waits for a key press?
wait
pause
sleep
hold
Answer: B) pause
Explanation:
The pause command suspends processing of the batch program until the user presses a key.
30. Which batch parameter represents the first argument passed to a batch file?
%0
%1
%2
%A
Answer: B) %1
Explanation:
%0 represents the batch file name, while %1, %2, and so on represent arguments passed to the batch file.
31. Which batch parameter represents all command-line arguments?
%*
%ALL%
%ARGS%
%9%
Answer: A) %*
Explanation:
The special batch parameter %* represents all arguments passed to the batch file after the batch-file name.
32. Which command shifts batch parameters so that %1 receives the previous %2 value?
rotate
shift
moveparam
advance
Answer: B) shift
Explanation:
The shift command changes the positions of batch parameters. It can also be used to process more than nine arguments by shifting additional arguments into %9.
33. Which special variable contains a pseudo-random integer generated by the command shell?
%RAND%
%RANDOM%
%RND%
%NUMBER%
Answer: B) %RANDOM%
Explanation:
%RANDOM% is a dynamic command-shell variable that produces a pseudo-random integer.
34. Which variable can be used to obtain the current directory in a batch script?
%PATH%
%CD%
%DIR%
%CURRENT%
Answer: B) %CD%
Explanation:
%CD% expands to the current directory in the Command Shell.
35. Which syntax correctly enables delayed environment-variable expansion?
set delayed=on
setlocal enabledelayedexpansion
enable !variables!
cmd /delay
Answer: B) setlocal enabledelayedexpansion
Explanation:
setlocal enabledelayedexpansion enables delayed variable expansion within the localized environment. Variables can then be expanded using !variable!.
36. Why is delayed expansion useful inside certain parenthesized blocks?
- It changes the file extension
- It allows variables to be expanded at execution time using
!variable!
- It disables all environment variables
- It converts batch files into executables
Answer: B) It allows variables to be expanded at execution time using !variable!
Explanation:
Normal percent expansion can occur when a compound command is parsed. Delayed expansion allows values to be expanded at execution time using exclamation marks, which is useful when a variable changes inside a loop or parenthesized block.
37. What is the purpose of setlocal?
- To start a network connection
- To localize environment changes within a batch script
- To create a local file
- To change the Windows language
Answer: B) To localize environment changes within a batch script
Explanation:
setlocal starts localization of environment-variable changes. The previous environment is restored when the corresponding endlocal is reached or the batch file ends.
38. Which command ends an environment-localization scope?
localend
endlocal
unsetlocal
restore
Answer: B) endlocal
Explanation:
endlocal ends the localization started by setlocal and restores the environment to its previous state.
39. Which command allows a batch script to present a list of single-character choices to a user?
select
choice
menu
input
Answer: B) choice
Explanation:
The choice command prompts the user to select one item from a list of single-character choices and sets ERRORLEVEL according to the selected choice.
40. What value does choice return for the first option?
- 0
- 1
- 2
- -1
Answer: B) 1
Explanation:
The first valid choice returns an ERRORLEVEL of 1, the second returns 2, and so on.
41. Which command calls a batch subroutine identified by a label?
call :label
goto :label
run :label
sub :label
Answer: A) call :label
Explanation:
With command extensions enabled, call :label invokes a labeled section as a batch subroutine. Execution can return to the calling location after the subroutine exits.
42. Which command can display the current date when used as echo %date%?
%time%
%date%
%today%
%calendar%
Answer: B) %date%
Explanation:
%date% is a dynamic command-shell variable representing the current date according to the system's regional settings.
43. Which command can display the current system time using environment-variable expansion?
echo %time%
echo %clock%
time show
print %clock%
Answer: A) echo %time%
Explanation:
%time% is a dynamic command-shell variable containing the current system time.
44. Which command sets the title of the Command Prompt window?
caption
windowtitle
title
name
Answer: C) title
Explanation:
The title command changes the title displayed by the Command Prompt window.
45. Which command is commonly used to concatenate the contents of multiple files?
join
merge
type
concat
Answer: C) type
Explanation:
The type command can output multiple files sequentially. With output redirection, this can be used to combine their contents into another file, such as type a.txt b.txt > combined.txt.
46. What does the pipe operator | do in the Windows Command Shell?
- Runs commands in reverse order
- Sends the output of one command to the input of another command
- Deletes the output of both commands
- Stores command output permanently
Answer: B) Sends the output of one command to the input of another command
Explanation:
The pipe operator | connects commands so that the standard output of the command on the left becomes input to the command on the right.
47. Which character is used as an escape character in the Windows Command Shell?
\
^
~
`
Answer: B) ^
Explanation:
The caret character ^ is commonly used to escape special characters in the Windows Command Shell. For example, ^& can represent a literal ampersand.
48. Consider the following batch script. What does it print?
@echo off
set name=Batch
if "%name%"=="Batch" (
echo Match
) else (
echo No Match
)
- No Match
- Batch
- Match
- ERROR
Answer: C) Match
Explanation:
The variable name contains Batch. After expansion, the condition becomes if "Batch"=="Batch", so the first branch executes.
49. Consider this batch script. What is printed?
@echo off
set count=1
:loop
echo %count%
set /a count+=1
if %count% LEQ 3 goto loop
- 1 2
- 1 2 3
- 0 1 2 3
- 3 only
Answer: B) 1 2 3
Explanation:
The script initially prints 1, increments the variable to 2, and loops. It then prints 2, increments to 3, and loops again. After printing 3, the value becomes 4, so the condition 4 LEQ 3 is false and the loop ends.
50. Consider the following batch script. What is the output?
@echo off
setlocal
set value=Original
setlocal
set value=Temporary
echo %value%
endlocal
echo %value%
endlocal
- Original followed by Original
- Temporary followed by Temporary
- Temporary followed by Original
- Original followed by Temporary
Answer: C) Temporary followed by Original
Explanation:
The inner setlocal creates a nested localization scope. Inside that scope, value is changed to Temporary. After the inner endlocal, the previous value, Original, is restored. The outer endlocal then ends the remaining localization scope.
Advertisement
Advertisement