CSC 202 (Python and PHP) Flashcards
What does PHP stand for?
i) Personal Home Page
ii) Hypertext Preprocessor
iii) Pretext Hypertext Processor
iv) Preprocessor Home Page
a) Both i) and iii)
b) Both ii) and iv)
c) Only ii)
d) Both i) and ii)
Answer: d
Explanation: PHP previously stood for Personal Home Page now stands for Hypertext Preprocessor.
What will be the output of the following PHP code?
<?php $num = 1; $num1 = 2; print $num . "+". $num1; ?>
a) 3
b) 1+2
c) 1.+.2
d) Error
Answer: b
Explanation: .(dot) is used to combine two parts of the statement. Example ($num . “Hello World”) will output 1Hello World
How should we add a single line comment in our PHP code?
i) /? ii) // iii) # iv) /* */
a) Only ii)
b) i), iii) and iv)
c) ii), iii) and iv)
d) Both ii) and iv)
Answer: c
Explanation: /* */ can also be use to comment just a single line although it is used for paragraphs. // and # are used only for single line comment
Which of the following PHP statement/statements will store 111 in variable num?
i) int $num = 111; ii) int mum = 111; iii) $num = 111; iv) 111 = $num;
a) Both i) and ii)
b) i), ii), iii) and iv)
c) Only iii)
d) Only i)
Answer: c
Explanation: You need not specify the datatype in php.
What will be the output of the following PHP code?
<?php $num = "1"; $num1 = "2"; print $num+$num1; ?>
a) 3
b) 1+2
c) Error
d) 12
Answer: a
Explanation: The numbers inside the double quotes are considered as integers and not string, therefore the value 3 is printed and not 1+2.
Which is the right way of declaring a variable in PHP?
i) $3hello
ii) $_hello
iii) $this
iv) $This
a) Only ii)
b) Only iii)
c) ii), iii) and iv)
d) ii) and iv)
Answer: d
Explanation: A variable in PHP can not start with a number, also $this is mainly used to refer properties of a class so we can’t use $this as a user define variable name.
advertise
What will be the output of the following PHP code?
<?php $foo = 'Bob'; $bar = &$foo; $bar = "My name is $bar"; echo $bar; echo $foo; ?>
a) Error
b) My name is BobBob
c) My name is BobMy name is Bob
d) My name is Bob Bob
Answer: c
Explanation: Firstly, the line $bar = &$foo; will reference $foo via $bar. So $bar is assigned value Bob. Therefore $bar = “My name is $bar”; will print My name is Bob($bar=Bob as said before).
Which of the following PHP statements will output Hello World on the screen?
i) echo (“Hello World”);
ii) print (“Hello World”);
iii) printf (“Hello World”);
iv) sprintf (“Hello World”);
a) i) and ii)
b) i), ii) and iii)
c) i), ii), iii) and iv)
d) i), ii) and iv)
Answer: b
Explanation: echo(), print() and printf() all three can be used to output a statement onto the screen. The sprintf() statement is functionally identical to printf() except that the output is assigned to a string rather than rendered to the browser.
What will be the output of the following PHP code?
<?php $color = "maroon"; $var = $color[2]; echo "$var"; ?>
a) a
b) Error
c) $var
d) r
Answer: d
Explanation: PHP treats strings in the same fashion as arrays, allowing for specific characters to be accessed via array offset notation. In an array, index always starts from 0. So in the line $var = $color[2]; if we count from start ‘r’ comes at index 2. So the output will be r.
What will be the output of the following PHP code?
<?php $score = 1234; $scoreboard = (array) $score; echo $scoreboard[0]; ?>
a) 1
b) Error
c) 1234
d) 2
Answer: c
Explanation: The (array) is a cast operator which is used for converting values from other data types to array.
What will be the output of the following PHP code?
<?php $total = "25 students"; $more = 10; $total = $total + $more; echo "$total"; ?>
a) Error
b) 35 students
c) 35
d) 25 students
Answer: c
Explanation: The integer value at the beginning of the original $total string is used in the calculation. However if it begins with anything but a numerical value, the value will be 0.
Which of the below statements is equivalent to $add += $add?
a) $add = $add
b) $add = $add +$add
c) $add = $add + 1
d) $add = $add + $add + 1
Answer: b
Explanation: a += b is an addition assignment whose outcome is a = a + b. Same can be done with subtraction, multiplication, division etc.
Which statement will output $x on the screen?
a) echo “$x”;
b) echo “$$x”;
c) echo “/$x”;
d) echo “$x;”;
Answer: a
Explanation: A backslash is used so that the dollar sign is treated as a normal string character rather than prompt PHP to treat $x as a variable. The backslash used in this manner is known as escape character.
What will be the output of the following PHP code?
<?php function track() { static $count = 0; $count++; echo $count; } track(); track(); track(); ?>
a) 123
b) 111
c) 000
d) 011
Answer: a
Explanation: Because $count is static, it retains its previous value each time the function is executed.
What will be the output of the following PHP code?
<?php $a = "clue"; $a .= "get"; echo "$a"; ?>
a) get
b) true
c) false
d) clueget
Answer: d
Explanation: ‘.’ is a concatenation operator. $a. = “get” is same as $a=$a.”get” where $a is having value of “clue” in the previous statement. So the output will be clueget.
What will be the output of the following PHP code?
<?php $a = "clue"; $a .= "get"; echo "$a"; ?>
a) get
b) true
c) false
d) clueget
Answer: d
Explanation: ‘.’ is a concatenation operator. $a. = “get” is same as $a=$a.”get” where $a is having value of “clue” in the previous statement. So the output will be clueget.
What will be the output of the following PHP code?
<?php $a = 5; $b = 5; echo ($a === $b); ?>
a) 5 === 5
b) Error
c) 1
d) False
Answer: c
Explanation: === operator returns 1 if $a and $b are equivalent and $a and $b have the same type.
Which of the below symbols is a newline character?
a) \r
b) \n
c) /n
d) /r
Answer: b
Explanation: PHP treats \n as a newline character.
What will be the output of the following PHP code?
<?php
$num = 10;
echo ‘What is her age? \n She is $num years old’;
?>
a) What is her age? \n She is $num years old
b) What is her age?
She is $num years old
c) What is her age? She is 10 years old
d) What is her age?
She is 10 years old
Answer: a
Explanation: When a string is enclosed within single quotes both variables and escape sequences will not be interpreted when the string is parsed.
Which of the conditional statements is/are supported by PHP?
i) if statements
ii) if-else statements
iii) if-elseif statements
iv) switch statements
a) Only i)
b) i), ii) and iv)
c) ii), iii) and iv)
d) i), ii), iii) and iv)
Answer: d
Explanation: All are conditional statements supported by PHP as all are used to evaluate different conditions during a program and take decisions based on whether these conditions evaluate to true of false