CSC 202 (Python and PHP) Flashcards

1
Q

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)

A

Answer: d
Explanation: PHP previously stood for Personal Home Page now stands for Hypertext Preprocessor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

Answer: b
Explanation: .(dot) is used to combine two parts of the statement. Example ($num . “Hello World”) will output 1Hello World

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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)

A

Answer: c
Explanation: You need not specify the datatype in php.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

Answer: c
Explanation: The (array) is a cast operator which is used for converting values from other data types to array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

Answer: b
Explanation: a += b is an addition assignment whose outcome is a = a + b. Same can be done with subtraction, multiplication, division etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which statement will output $x on the screen?
a) echo “$x”;
b) echo “$$x”;
c) echo “/$x”;
d) echo “$x;”;

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

Answer: a
Explanation: Because $count is static, it retains its previous value each time the function is executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

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

A

Answer: c
Explanation: === operator returns 1 if $a and $b are equivalent and $a and $b have the same type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Which of the below symbols is a newline character?
a) \r
b) \n
c) /n
d) /r

A

Answer: b
Explanation: PHP treats \n as a newline character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Which of the looping statements is/are supported by PHP?

i) for loop
ii) while loop
iii) do-while loop
iv) foreach loop

a) i) and ii)
b) i), ii) and iii)
c) i), ii), iii) and iv)
d) Only iv)

A

Answer: c
Explanation: All are supported looping statements in PHP as they can repeat the same block of code a given number of times, or until a certain condition is met.

22
Q

What will be the output of the following PHP code?

<?php
$team = "arsenal";
switch ($team) {
case "manu":
    echo "I love man u";
case "arsenal":
    echo "I love arsenal";
case "manc":
    echo "I love manc"; }
?>

a) I love arsenal
b) Error
c) I love arsenalI love manc
d) I love arsenalI love mancI love manu

A

Answer: c
Explanation: If a break statement isn’t present, all subsequent case blocks will execute until a break statement is located.

23
Q

What will be the output of the following PHP code?

<?php
$user = array("Ashley", "Bale", "Shrek", "Blank");
for ($x=0; $x < count($user); $x++)	{
    if ($user[$x] == "Shrek") continue;
        printf ($user[$x]); 
}
?>

a) AshleyBale
b) AshleyBaleBlank
c) ShrekBlank
d) Shrek

A

Answer: b
Explanation: The continue statement causes execution of the current loop iteration to end and commence at the beginning of the next iteration.

24
Q

If $a = 12, what will be returned when ($a == 12) ? 5 : 1 is executed?
a) 12
b) 1
c) Error
d) 5

A

Answer: d
Explanation: ?: is known as ternary operator. If condition is true then the part just after the ? is executed else the part after : .

25
Q

What will be the value of $a and $b after the function call in the following PHP code?

<?php
function doSomething( &$arg ) {
    $return = $arg;
    $arg += 1;
    return $return;	
}
$a = 3;
$b = doSomething( $a );
?>

a) a is 3 and b is 4
b) a is 4 and b is 3
c) Both are 3
d) Both are 4

A

Answer: b
Explanation: $a is 4 and $b is 3. The former because $arg is passed by reference, the latter because the return value of the function is a copy of the initial value of the argument.

26
Q

Who is the father of PHP?
a) Rasmus Lerdorf
b) Willam Makepiece
c) Drek Kolkevi
d) List Barely

A

Answer: a
Explanation: PHP was originally created by Rasmus Lerdorf in 1994.

27
Q

What is the result of cmp(3, 1)?
a) 1
b) 0
c) True
d) False

A

Answer: a
Explanation: cmp(x, y) returns 1 if x > y, 0 if x == y and -1 if x < y.

28
Q

Which of the following is incorrect?
a) float(‘inf’)
b) float(‘nan’)
c) float(’56’+’78’)
d) float(’12+34′)

A

Answer: d
Explanation: ‘+’ cannot be converted to a float.

29
Q

What is the result of round(0.5) – round(-0.5)?
a) 1.0
b) 2.0
c) 0.0
d) Value depends on Python version

A

Answer: d
Explanation: The behavior of the round() function is different in Python 2 and Python 3. In Python 2, it rounds off numbers away from 0 when the number to be rounded off is exactly halfway through. round(0.5) is 1 and round(-0.5) is -1 whereas in Python 3, it rounds off numbers towards nearest even number when the number to be rounded off is exactly halfway through. See the below output.

30
Q

What is the result of round(0.5) – round(-0.5)?
a) 1.0
b) 2.0
c) 0.0
d) Value depends on Python version

A

Answer: d
Explanation: The behavior of the round() function is different in Python 2 and Python 3. In Python 2, it rounds off numbers away from 0 when the number to be rounded off is exactly halfway through. round(0.5) is 1 and round(-0.5) is -1 whereas in Python 3, it rounds off numbers towards nearest even number when the number to be rounded off is exactly halfway through. See the below output.

31
Q

What does 3 ^ 4 evaluate to?
a) 81
b) 12
c) 0.75
d) 7

A

Answer: d
Explanation: ^ is the Binary XOR operator.

32
Q
  1. Evaluate the expression given below if A = 16 and B = 15

.A % B // A
a) 0.0
b) 0
c) 1.0
d) 1

A

Answer: b
Explanation: The above expression is evaluated as 16%15//16, which is equal to 1//16, which results in 0.

33
Q

What is the value of the following expression?

8/4/2, 8/(4/2)
a) (1.0, 4.0)
b) (1.0, 1.0)
c) (4.0. 1.0)
d) (4.0, 4.0)

A

Answer: a
Explanation: The above expressions are evaluated as: 2/2, 8/2, which is equal to (1.0, 4.0).

34
Q

Which of the following expressions results in an error?

a) float(‘10’)
b) int(‘10’)
c) float(’10.8’)
d) int(’10.8’)

A

Answer : D
Explanation:
a) float(‘10’): This converts the string ‘10’ into a float, resulting in 10.0. No error occurs.
b) int(‘10’): This converts the string ‘10’ into an integer, resulting in 10. No error occurs.
c) float(‘10.8’): This converts the string ‘10.8’ into a float, resulting in 10.8. No error occurs.
d) int(‘10.8’): This tries to convert the string ‘10.8’ into an integer. However, ‘10.8’ is not a valid integer string (since it includes a decimal point), so this results in a ValueError.

35
Q

A function in PHP which starts with __ (double underscore) is known as __________
a) Magic Function
b) Inbuilt Function
c) Default Function
d) User Defined Function

A

Answer: a
Explanation: PHP functions that start with a double underscore – a “__” – are called magic functions in PHP. They are functions that are always defined inside classes, and are not stand-alone functions.

36
Q

What will be the output of the following PHP code?

<?php
$op2 = "blabla";
function foo($op1)
{
    echo $op1;
    echo $op2;
}
foo("hello");
?>

a) helloblabla
b) Error
c) hello
d) helloblablablabla

A

Answer: c
Explanation: If u want to put some variables in function that was not passed by it, you must use “global”. Inside the function type global $op2.

37
Q

What will be the output of the following PHP code?

<?php
$op2 = "blabla";
function foo($op1)
{
    echo $op1;
    echo $op2;
}
foo("hello");
?>

a) helloblabla
b) Error
c) hello
d) helloblablablabla

A

Answer: c
Explanation: If u want to put some variables in function that was not passed by it, you must use “global”. Inside the function type global $op2.

38
Q

What will be the output of the following PHP code?

<?php
function a()
{
    function b()
    {
        echo 'I am b';
 	}
    echo 'I am a';
}
a();
a();
?>

a) I am a
b) I am bI am a
c) Error
d) I am a Error

A

Answer: d
Explanation: Since we are calling the function a() 2 times, the output will be “I am a” followed by a Fatal Error as we are redeclaring function b() during second call of function a().

39
Q

What will be the output of the following PHP code?

<?php
function a()
{
    function b()
    {
        echo 'I am b';
 	}
    echo 'I am a';
}
a();
a();
?>

a) I am a
b) I am bI am a
c) Error
d) I am a Error

A

Answer: d
Explanation: Since we are calling the function a() 2 times, the output will be “I am a” followed by a Fatal Error as we are redeclaring function b() during second call of function a().

40
Q

Type Hinting was introduced in which version of PHP?
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6

A

Answer: b
Explanation: PHP 5 introduced the feature of type hinting. With the help of type hinting, we can specify the expected data type of an argument in a function declaration. First valid types can be the class names for arguments that receive objects and the other are array for those that receive arrays.

41
Q

Type Hinting was introduced in which version of PHP?
a) PHP 4
b) PHP 5
c) PHP 5.3
d) PHP 6

A

Answer: b
Explanation: PHP 5 introduced the feature of type hinting. With the help of type hinting, we can specify the expected data type of an argument in a function declaration. First valid types can be the class names for arguments that receive objects and the other are array for those that receive arrays.

42
Q

Which of the following are valid function names?

i) function()
ii) €()
iii) .function()
iv) $function()

a) Only i)
b) Only ii)
c) i) and ii)
d) iii) and iv)

A

Answer: b
Explanation: A valid function name can start with a letter or underscore, followed by any number of letters, numbers, or underscores. According to the specified regular expression ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*), a function name like this one is valid.

43
Q

Which of the following PHP functions accepts any number of parameters?
a) func_get_argv()
b) func_get_args()
c) get_argv()
d) get_argc()

A

Answer: b
Explanation: func_get_args() returns an array of arguments provided. One can use func_get_args() inside the function to parse any number of passed parameters.

44
Q

Which of the following PHP functions can be used to get the current memory usage?
a) get_usage()
b) get_peak_usage()
c) memory_get_usage()
d) memory_get_peak_usage()

A

Answer: c
Explanation: memory_get_usage() returns the amount of memory, in bytes, that’s currently being allocated to the PHP script

45
Q

Which one of the following functions can be used to compress a string?
a) zip_compress()
b) zip()
c) compress()
d) gzcompress()

A

Answer: d
Explanation: The function gzcompress() compresses the string using the ZLIB data format. One can achieve upto 50% size reduction using this function. The gzuncompress() function is used to uncompress the string.

46
Q

Which one of the following functions can be used to compress a string?
a) zip_compress()
b) zip()
c) compress()
d) gzcompress()

A

Answer: d
Explanation: The function gzcompress() compresses the string using the ZLIB data format. One can achieve upto 50% size reduction using this function. The gzuncompress() function is used to uncompress the string.

47
Q

<?php
echo chr(52);
?>

a) 1
b) 2
c) 3
d) 4

A

Answer: d
Explanation: The chr() function returns a character from the specified ASCII value. We can specify ASCII value in decimal, octal, or hex values. The Octal values are defined as a leading 0, while hex values are defined as a leading 0x. Since the ASCII value of 4 is 52, thus 4 was displayed

48
Q

<?php
echo chr(52);
?>

a) 1
b) 2
c) 3
d) 4

A

Answer: d
Explanation: The chr() function returns a character from the specified ASCII value. We can specify ASCII value in decimal, octal, or hex values. The Octal values are defined as a leading 0, while hex values are defined as a leading 0x. Since the ASCII value of 4 is 52, thus 4 was displayed

49
Q

What will be the output of the following PHP code?

<?php
echo ord (“hi”);
?>

a) 106
b) 103
c) 104
d) 209

A

Answer: c
Explanation: The ord() function returns the ASCII value of the first character of a string. The ASCII value of h is 104, thus 104 was displayed

50
Q

What will be the output of the following PHP code?

<?php
echo ucwords("i love my country");
?>

a) I love my country
b) i love my Country
c) I love my Country
d) I Love My Country

A

Answer: d
Explanation: The ucwords() function converts the first character of each word in a string to uppercase.

51
Q

What will be the output of the following PHP code?

<?php
echo lcfirst("welcome to India");
?>

a) welcome to India
b) welcome to india
c) Welcome to India
d) Welcome to india

A

Answer: a
Explanation: The lcfirst() function converts the first character of a string to lowercase.