循环语句与条件语句_PHP条件语句能力问题和解答

循环语句与条件语句

This section contains Aptitude Questions and Answers on PHP Conditional Statements.

本节包含有关PHP条件语句的 Aptitude问题和解答。

1) Which of the following statements are conditional statements in PHP?
  1. If statements

  2. If else statements

  3. Switch statements

  4. Loop statements

Options:

  1. A and B

  2. C

  3. A, B, and C

  4. A, B, C, and D

Answer & Explanation

Correct answer: 3
A, B, and C

Explanation:

The IF, IF-ELSE, and SWITCH are conditional statements, while LOOPs are iterative statements in PHP.

1)以下哪个语句是PHP中的条件语句?
  1. 如果陈述

  2. 如果是其他陈述

  3. 切换语句

  4. 循环语句

选项:

  1. A和B

  2. C

  3. A,B和C

  4. A,B,C和D

答案与解释

正确答案:3
A,B和C

说明:

IF,IF-ELSE和SWITCH是条件语句,而LOOP是PHP中的迭代语句。

2) What is the correct output of the given code snippets?
<?php
  $num = 20;
  
  if ($num) 
  {
    echo "Hello";
  }
  else
  {
  	echo "Hii";
  }
?>

  1. Hello

  2. Hi

  3. Error

  4. None of the above

Answer & Explanation

Correct answer: 1
Hello

Explanation:

The above code will print "Hello" on the webpage. Because variable $num contains 20, then every non-zero value is treated as a true condition for if statements in PHP.

2)给定代码段的正确输出是什么?
  1. 你好

  2. 你好

  3. 错误

  4. 以上都不是

答案与解释

正确答案:1
你好

说明:

上面的代码将在网页上打印“ Hello” 。 因为变量$ num包含20,所以每个非零值都将视为PHP中if语句的真实条件。

3) What is the correct output of the given code snippets?
<?php
  
  if (echo("India ")) 
  {
    echo "Hello";
  }
  else
  {
  	echo "Hii";
  }
?>

  1. India Hello

  2. India Hii

  3. Error

  4. None of the above

Answer & Explanation

Correct answer: 3
Error

Explanation:

The above code will generate syntax error. Because echo () function does not return any value.

3)给定代码段的正确输出是什么?
  1. 印度你好

  2. 印度Hii

  3. 错误

  4. 以上都不是

答案与解释

正确答案:3
错误

说明:

上面的代码将生成语法错误。 因为echo()函数不会返回任何值。

4) What is the correct output of the given code snippets?
<?php
  
  if (print("India ")) 
  {
    echo "Hello";
  }
  else
  {
  	echo "Hii";
  }
?>

  1. India Hello

  2. India Hii

  3. Error

  4. None of the above

Answer & Explanation

Correct answer: 3
India Hello

Explanation:

The code is correct, it will print "India Hello" on the webpage.

The print() function returns the number of characters printed on the webpage, then the returned value will 6, then the IF condition will be true.

4)给定代码段的正确输出是什么?
  1. 印度你好

  2. 印度Hii

  3. 错误

  4. 以上都不是

答案与解释

正确答案:3
印度你好

说明:

代码正确,它将在网页上打印“ India Hello”。

print()函数返回在网页上打印的字符数,然后返回值将为6,然后IF条件为true。

5) What is the correct output of the given code snippets?
<?php  
  if (true) 
  {
    echo "Hello";
  }
  else
  {
  	echo "Hii";
  }
?>

  1. Hello

  2. Hii

  3. Error

  4. None of the above

Answer & Explanation

Correct answer: 1
Hello

Explanation:

The above code will print "Hello" on the webpage. Here we explicitly passed true, then the if condition gets true.

5)给定代码段的正确输出是什么?
  1. 你好

  2. i

  3. 错误

  4. 以上都不是

答案与解释

正确答案:1
你好

说明:

上面的代码将在网页上打印“ Hello”。 在这里,我们显式传递了true ,则if条件变为true。

6) What is the correct output of the given code snippets?
<?php  
  if (0) 
  {
    echo "Hello";
  }
  else
  {
  	echo "Hii";
  }
?>

  1. Hello

  2. Hii

  3. Error

  4. None of the above

Answer & Explanation

Correct answer: 2
Hii

Explanation:

The above code will print "Hii" on the webpage. Because here we pass 0 in the if condition, and we know that the 0 represents the false condition.

6)给定代码段的正确输出是什么?
  1. 你好

  2. i

  3. 错误

  4. 以上都不是

答案与解释

正确答案:2
i

说明:

上面的代码将在网页上打印“ Hii”。 因为在这里,我们在if条件中传递了0 ,所以我们知道0代表错误条件。

7) What is the correct output of the given code snippets?
<?php  
  $num1 = 10;
  $num2 = 20;

  if ($num1 > $num2) 
    echo "ABC";
    echo "PQR";
  else
  	echo "XYZ";
?>

  1. ABC PQR

  2. XYZ

  3. Error

  4. None of the above

Answer & Explanation

Correct answer: 3
Error

Explanation:

The above code will generate a syntax error because the only single statement is allowed without curly brasses {} in the IF statement.

7)给定代码段的正确输出是什么?
  1. 美国广播公司

  2. XYZ

  3. 错误

  4. 以上都不是

答案与解释

正确答案:3
错误

说明:

上面的代码将产生语法错误,因为在IF语句中只允许使用单个语句而不使用大括号{} 。

8) What is the correct output of the given code snippets?
<?php  
  $num1 = 10;
  $num2 = 20;
  $num3 = 30;

  if ($num1 > $num2 && $num1>$num3) 
    echo $num1;
  else if($num2 > $num1 && $num2>$num3) 
  	echo $num2;
  else
  	echo $num3;
?>

  1. 10

  2. 20

  3. 30

  4. Error

Answer & Explanation

Correct answer: 3
30

Explanation:

The above code will print "30" on the webpage. Because $num1 and $num2 contains 10 and 20 respectively those are smaller than $num3, so that 1st and 2nd conditions get false. Then the else part gets executed.

8)给定代码段的正确输出是什么?
  1. 10

  2. 20

  3. 30

  4. 错误

答案与解释

正确答案:3
30

说明:

上面的代码将在网页上打印“ 30”。 因为$ num1和$ num2分别包含10个和20个,分别小于$ num3 ,所以1st和2nd条件变为 false。 然后,其他部分被执行。

9) What is the correct output of the given code snippets?
<?php  
  $num1 = 10;
  $num2 = 20;
  $num3 = 30;

  if ($num1 > $num2 && $num1>$num3) 
    echo $num1;
  elseif($num2 > $num1 && $num2>$num3) 
  	echo $num2;
  else
  	echo $num3;
?>

  1. 10

  2. 20

  3. 30

  4. Error

Answer & Explanation

Correct answer: 3
30

Explanation:

The above code will print "30" on the webpage. Because $num1 and $num2 contains 10 and 20 respectively those are lower than $num3, so that 1st and 2nd conditions get false. Then the else part gets executed. And we can use else if or elseif both for conditional statements.

9)给定代码段的正确输出是什么?
  1. 10

  2. 20

  3. 30

  4. 错误

答案与解释

正确答案:3
30

说明:

上面的代码将在网页上打印“ 30”。 因为$ num1和$ num2分别包含10个和20个,分别小于$ num3 ,所以1st和2nd条件变为 false。 然后,其他部分被执行。 我们可以将else if或elseif都用于条件语句。

10) What is the correct output of the given code snippets?
<?php  
  $num1 = 10;
  $num2 = 20;
  $num3 = 30;

  if ($num1 > $num2 and $num1>$num3) 
    echo $num1;
  elseif($num2 > $num1 and $num2>$num3) 
  	echo $num2;
  else
  	echo $num3;
?>

  1. 10

  2. 20

  3. 30

  4. Error

Answer & Explanation

Correct answer: 3
30

Explanation:

The above code will print "30" on the webpage. Because $num1 and $num2 contains 10 and 20 respectively, those are lower than $num3, so that 1st and 2nd conditions get false. Then the else part gets executed. And we can use and or && both for checking multiple conditions in PHP.

10)给定代码段的正确输出是什么?
  1. 10

  2. 20

  3. 30

  4. 错误

答案与解释

正确答案:3
30

说明:

上面的代码将在网页上打印“ 30”。 因为$ num1和$ num2分别包含10和20,所以它们小于$ num3 ,因此1st和2nd条件变为 false。 然后,其他部分被执行。 而且我们可以使用和或&&&两者来检查PHP中的多个条件。

11) What is the correct output of the given code snippets?
<?php  
  if (10 and 20) 
    	echo "ABC";
  elseif(0 and -1) 
  	echo "PQR";
  else
  	echo "XYZ";
?>

  1. ABC

  2. PQR

  3. XYZ

  4. Error

Answer & Explanation

Correct answer: 1
ABC

Explanation:

The above code will print "ABC" on the webpage. Because in the if condition both values 10, 20 are non-zero, that's why condition gets true.

11)给定代码段的正确输出是什么?
  1. 美国广播公司

  2. PQR

  3. XYZ

  4. 错误

答案与解释

正确答案:1
美国广播公司

说明:

上面的代码将在网页上打印“ ABC”。 因为在if条件中,两个值10、20都不为零,这就是为什么条件变为true的原因。

12) What is the correct output of the given code snippets?
<?php  
  if(0 and -1) 
    echo "ABC";
  else if (10 and 20) 
  	echo "PQR";
  else
  	echo "XYZ";
?>

  1. ABC

  2. PQR

  3. XYZ

  4. Error

Answer & Explanation

Correct answer: 2
PQR

Explanation:

The above code will print the "PQR" on the webpage. Because IF condition gets false due to 0, and else if condition gets true because 10 and 20 both are non zero.

12)给定代码段的正确输出是什么?
  1. 美国广播公司

  2. PQR

  3. XYZ

  4. 错误

答案与解释

正确答案:2
PQR

说明:

上面的代码将在网页上打印“ PQR”。 因为IF条件由于0而变为假, 否则因10和20都不为零而使条件成立。

13) What is the correct output of the given code snippets?
<?php  
  	if(print("ABC ") and print("PQR ")) 
	    echo "TUV";
	else
  	   echo "XYZ";
?>

  1. ABC PQR TUV

  2. ABC PQR XYZ

  3. ABC TUV

  4. ABC XYZ

Answer & Explanation

Correct answer: 1
ABC PQR TUV

Explanation:

The above code will print "ABC PQR TUV" on the web page.

The both function returns a non-zero value, so that and operation of both is true.

13)给定代码段的正确输出是什么?
 <?php  
  	if ( print ( "ABC " ) and print ( "PQR " )) 
	    echo "TUV" ;
	else
  	   echo "XYZ" ;
?>

  1. ABC PQR TUV

  2. ABC PQR XYZ

  3. ABC TUV

  4. ABC XYZ

答案与解释

正确答案:1
ABC PQR TUV

说明:

上面的代码将在网页上打印“ ABC PQR TUV”。

两个函数都返回一个非零值,因此和运算都为true。

14) What is the correct output of the given code snippets?
<?php
if (print ("ABC ") or print ("PQR ")) echo "TUV";
else echo "XYZ";
?>

  1. ABC PQR TUV

  2. ABC PQR XYZ

  3. ABC TUV

  4. ABC XYZ

Answer & Explanation

Correct answer: 3
ABC TUV

Explanation:

The above code will print "ABC TUV" on the web page. Because in case of or operation, if the first condition is true then it does not check 2nd condition so that it prints "ABC TUV".

14)给定代码段的正确输出是什么?
  1. ABC PQR TUV

  2. ABC PQR XYZ

  3. ABC TUV

  4. ABC XYZ

答案与解释

正确答案:3
ABC TUV

说明:

上面的代码将在网页上打印“ ABC TUV”。 因为在或操作的情况下,如果第一个条件为true,则它不会检查第二个条件,因此它会打印“ ABC TUV”。

15) What is the correct output of the given code snippets?
<?php
if (print ("ABC ") xor print ("PQR ")) echo "TUV";
else echo "XYZ";
?>

  1. ABC PQR TUV

  2. ABC PQR XYZ

  3. ABC TUV

  4. Error

Answer & Explanation

Correct answer: 2
ABC PQR XYZ

Explanation:

The XOR of 4 and 4 is 0, then the if condition gets false. So that it will print "ABC PQR XYZ".

15)给定代码段的正确输出是什么?
 <?php
if ( print ( "ABC " ) xor print ( "PQR " )) echo "TUV" ;
else echo "XYZ" ;
?>

  1. ABC PQR TUV

  2. ABC PQR XYZ

  3. ABC TUV

  4. 错误

答案与解释

正确答案:2
ABC PQR XYZ

说明:

4和4的XOR为0,则if条件变为false。 这样它将打印“ ABC PQR XYZ”。

16) What is the correct output of the given code snippets?
<?php  
  if(print("ABC ") xor print('PQR ')) 
    echo "TUV;
  else
  	echo "XYZ";
?>

  1. ABC PQR TUV

  2. ABC PQR XYZ

  3. ABC TUV

  4. Error

Answer & Explanation

Correct answer: 4
Error

Explanation:

The above code will generate an error because the closing double quote is missing in the 3rd line.

16)给定代码段的正确输出是什么?
  1. ABC PQR TUV

  2. ABC PQR XYZ

  3. ABC TUV

  4. 错误

答案与解释

正确答案:4
错误

说明:

上面的代码将产生错误,因为第三行中缺少右引号。

17) What is the correct output of the given code snippets?
<?php
if (true & print ("PQR ")) echo "TUV";
else echo "XYZ";
?>

  1. PQR 1TUV

  2. PQR 0TUV

  3. PQR TUV

  4. None of the above

Answer & Explanation

Correct answer: 3
PQR TUV

17)给定代码段的正确输出是什么?
  1. PQR 1TUV

  2. PQR 0TUV

  3. PQR TUV

  4. 以上都不是

答案与解释

正确答案:3
PQR TUV

18) What is the correct output of the given code snippets?
<?php
if (true & true) if (true) echo "ABC";
else echo "PQR";
else echo "XYZ";
?>

  1. ABC

  2. PQR

  3. XYZ

  4. None of the above

Answer & Explanation

Correct answer: 1
ABC

Explanation:

The "ABC" will be print because if(true & true) is true and then if(true) is also true.

18)给定代码段的正确输出是什么?
 <?php
if ( true & true ) if ( true ) echo "ABC" ;
else echo "PQR" ;
else echo "XYZ" ;
?>

  1. 美国广播公司

  2. PQR

  3. XYZ

  4. 以上都不是

答案与解释

正确答案:1
美国广播公司

说明:

将打印“ ABC”,因为if(true&true)为true,然后if(true)也为true。

19) What is the correct output of the given code snippets?
<?php
switch (1)
{
    case 0:
        echo "ABC ";
    case 1:
        echo "PQR ";
    case 2:
        echo "TUV ";
    default:
        echo "XYZ ";
}
?>

  1. PQR

  2. PQR TUV XYZ

  3. Error

  4. None of the above

Answer & Explanation

Correct answer: 2
PQR TUV XYZ

Explanation:

The above code will print the "PQR TUV XYZ". Because here we did not use break statement, then the case 1, case 2 and case 3 will be executed.

19)给定代码段的正确输出是什么?
  1. PQR

  2. PQR TUV XYZ

  3. 错误

  4. 以上都不是

答案与解释

正确答案:2
PQR TUV XYZ

说明:

上面的代码将打印“ PQR TUV XYZ”。 因为这里我们没有使用break语句,所以将执行案例1 , 案例2和案例3 。

20) What is the correct output of the given code snippets?
<?php  
    switch(1)
    {
            default:
        		echo "XYZ ";
            break;
            
            case 0:
        		echo "ABC ";
            break;
            
            case 1:
        		echo "PQR ";
            break;
            
            case 2:
        		echo "TUV ";
            break;
    }
?>

  1. PQR

  2. PQR TUV XYZ

  3. Error

  4. None of the above

Answer & Explanation

Correct answer: 1
PQR

Explanation:

The above will print "PQR" on the webpage, because here case 1 is the exact match.

20)给定代码段的正确输出是什么?
  1. PQR

  2. PQR TUV XYZ

  3. 错误

  4. 以上都不是

答案与解释

正确答案:1
PQR

说明:

上面将在网页上打印“ PQR”,因为这里的情况1是完全匹配的。

21) What is the correct output of the given code snippets?
<?php
switch ('1')
{
    default:
        echo "XYZ ";
    break;
    case 0:
        echo "ABC ";
    break;
    case 1:
        echo "PQR ";
    break;
    case 2:
        echo "TUV ";
    break;
}
?>

  1. PQR

  2. PQR TUV XYZ

  3. Error

  4. None of the above

Answer & Explanation

Correct answer: 1
PQR

The above will print "PQR" on the webpage, because here case 1 will be executed, because 1 and '1' will both work the same in the switch.

21)给定代码段的正确输出是什么?
  1. PQR

  2. PQR TUV XYZ

  3. 错误

  4. 以上都不是

答案与解释

正确答案:1
PQR

上面将在网页上打印“ PQR”,因为此处将执行情况1 ,因为1和“ 1”在开关中都将相同地工作。

22) What is the correct output of the given code snippets?
<?php
switch ('1')
{
    default:
        echo "XYZ ";
    break;
    case "0":
        echo "ABC ";
    break;
    case "1":
        echo "PQR ";
    break;
    case "2":
        echo "TUV ";
    break;
}
?>

  1. PQR

  2. PQR TUV XYZ

  3. Error

  4. None of the above

Answer & Explanation

Correct answer: 1
PQR

The above will print "PQR" on the webpage, because here case 1 will be executed, because 1, '1', and "1" will all work the same in the switch.

22)给定代码段的正确输出是什么?
  1. PQR

  2. PQR TUV XYZ

  3. 错误

  4. 以上都不是

答案与解释

正确答案:1
PQR

上面将在网页上打印“ PQR”,因为在此情况下将执行情况1 ,因为1,“ 1”和“ 1”在开关中的作用相同。

23) What is the correct output of the given code snippets?
<?php
switch ('1')
{
    default:
        echo "XYZ ";
    case 0:
        echo "ABC ";
    break;
    case 1:
        echo "PQR ";
    break;
    case 2:
        echo "TUV ";
    break;
}
?>

  1. PQR

  2. XYZ PQR

  3. Error

  4. None of the above

Answer & Explanation

Correct answer: 1
PQR

Explanation:

The above will print "PQR" on the webpage, here the break statement is missing in the default case, but in the above code default case will not execute, so that it will print "PQR".

23)给定代码段的正确输出是什么?
  1. PQR

  2. XYZ PQR

  3. 错误

  4. 以上都不是

答案与解释

正确答案:1
PQR

说明:

上面将在网页上打印“ PQR”,此处默认情况下缺少break语句,但在上面的代码中默认情况下将不执行,因此将打印“ PQR”。

24) What is the correct output of the given code snippets?
<?php  
    switch(1)
        case 1:
        	echo "PQR ";
        break;
    
?>

  1. PQR

  2. Error

Answer & Explanation

Correct answer: 2
Error

Explanation:

The above code will generate syntax error, because we cannot use switch without curly brasses {}.

24)给定代码段的正确输出是什么?
  1. PQR

  2. 错误

答案与解释

正确答案:2
错误

说明:

上面的代码将产生语法错误,因为如果没有大括号{} ,我们将无法使用switch。

25) What is the correct output of the given code snippets?
<?php
$num = 2;
switch (num)
{
    case 0:
        echo "ABC ";
    break;
    case 1:
        echo "PQR ";
    break;
    case 2:
        echo "TUV ";
    break;
    default:
        echo "XYZ ";
    break;
}
?>

  1. ABC

  2. XYZ

  3. Error

  4. None of the above

Answer & Explanation

Correct answer: 1
ABC

Explanation:

The above code will print "ABC" on the webpage. Because we pass variable num instead of $num then num is treated as '0' then case 0 is executed.

25)给定代码段的正确输出是什么?
  1. 美国广播公司

  2. XYZ

  3. 错误

  4. 以上都不是

答案与解释

正确答案:1
美国广播公司

说明:

上面的代码将在网页上打印“ ABC”。 因为我们传递变量num而不是$ num,所以将num视为'0',然后执行情况0 。

翻译自: https://www.includehelp.com/php/conditional-statements-aptitude-questions-and-answers.aspx

循环语句与条件语句

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值