RSS Feed

Multiple Choice Questions - Local Variables

Multiple Choice Questions - Local Variables

1. The scope of a local variable is the batch in which it is declared.

A) True
B) False

2. What will be the output of the below snippet:
DECLARE @count INT
SET @count = 1

WHILE @count <=3
BEGIN

DECLARE @var INT
SET @var = ISNULL(@var,0) + 1
SELECT @var
SET @count = @count +1

END
A) Count variable is not accessible inside Begin/End, so nothing will be printed.
B) It will Print below output on screen:
     1
     2
     3
C) It will print only 1 on screen.
D) It will show an error message.

3. To declare more than one local variable, use a . . . . after the first local variable defined, and then specify the next local variable name and data type.

A) Slash
B) Comma
C) Semi colon
D) Full stop

4. Which of the following statements will execute without error:

A)
DECLARE @MyVariable int;
SET @MyVariable = 1;
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'SELECT ' + CONVERT(VARCHAR(12), @MyVariable);
EXECUTE sp_executesql @sql;
B)
DECLARE @MyVariable int;
SET @MyVariable = 1;
EXECUTE sp_executesql N'SELECT @MyVariable';
C)
DECLARE @MyVariable int;
SET @MyVariable = 1;
EXECUTE sp_executesql N'SELECT ' + @MyVariable;
D)
DECLARE @MyVariable int;
SET @MyVariable = 1;
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'SELECT ' + CONVERT(NCHAR(12), @MyVariable);
EXECUTE sp_executesql @sql;

5. Which of the following code will NOT work:
A)
DECLARE  @Total_income  int ;
SET  @Total_income  =  10;
SET  @Total_income  =  convert (text, @Total_income)  *  10;
SELECT  @Total_income;
B)
DECLARE  @Total_income  int ;
SET  @Total_income  =  10;
SET  @Total_income  =  convert (int, @Total_income)  *  10;
SELECT  @Total_income;
C)
DECLARE  @Total_income  int ;
SET  @Total_income  =  10;
SET  @Total_income  =  @Total_income * @Total_income *  10 * @Total_income;
SELECT  @Total_income;
D)
DECLARE  @Total_income  int ;
SET  @Total_income  =  10;
SET  @Total_income  =  @Total_income  *  10;
SELECT  @Total_income;

6. Select the correct output of the below code:
DECLARE @p int;
SET @p = @p + 1;
SELECT @p;
GO
A) 1
B) 0
C) NULL
D) It will show an error message

7. Below two statements will run:
DECLARE @var INT
DECLARE @var Varchar(12)
A) Successfully
B) Unsuccessfully

8. What is the result of running the below code:
DECLARE @Var_1 INT
DECLARE @Var_2 INT
DECLARE @Temporary_var INT
SET @Var_1 = 3.1
SET @Var_2 = 4.9
SELECT @Var_1 Var_1, @Var_2 Var_2
SELECT @Temporary_var = @Var_1, @Var_1 = @Var_2, @Var_2 = @Temporary_var
SELECT @Var_1 Var_1, @Var_2 Var_2
GO
A)
Var_1 Var_2
3 4

Var_1 Var_2
4 3

B)
It will show an error as int cannot store decimal numbers.

C)
Var_1 Var_2
3.0 4.0

Var_1 Var_2
4.0 3.0

D)
Var_1 Var_2
3.1 4.9

Var_1 Var_2
4.9 3.1


9. What will be the value of @Var2:
BEGIN TRANSACTION
DECLARE @Var_1 INT, @Var_2 INT
SET @Var_1 = 7
ROLLBACK TRANSACTION
PRINT @Var_1
BEGIN
SET @Var_1 = 2
PRINT @Var_1
END
SET @Var_2 = @Var_1
PRINT @Var_2
GO
A) 7
B) 2
C) NULL
D) 0

10. What is the result of below code:
DECLARE @FlagINT INT

SET @FlagInt = 1

BEGIN transaction
SET @FlagInt = NULL
ROLLBACK TRANSACTION

SELECT @FlagInt AS RESULT
GO
A) 1
B) It will show an error message
C) 0
D) Null

Answers