1.
What will be printed?
if ('2' == '02') {
echo 'true';
} else {
echo 'false';
}
Correct Answer
A. True
Explanation
The code compares the string '2' with the string '02'. The double equals (==) operator in PHP performs type coercion, which means it converts the operands to the same type before comparing them. In this case, both strings are converted to integers. Since both '2' and '02' represent the same integer value (2), the comparison returns true. Therefore, the code will print 'true'.
2.
Which of the following is NOT a valid pHP comparison operator?
Correct Answer
C.
Explanation
The comparison operators in PHP are used to compare two values and determine their relationship. The "!=" operator is a valid comparison operator in PHP and it checks if two values are not equal. The ">=" operator is also a valid comparison operator and it checks if the left operand is greater than or equal to the right operand. The "===" operator is another valid comparison operator in PHP and it checks if two values are identical in terms of both value and data type. Therefore, the answer is not available.
3.
What will be printed?
$a = array(
null => 'a',
true => 'b',
false => 'c',
0 => 'd',
1 => 'e',
'' => 'f'
);
echo count($a), "\n";
Correct Answer
B. 3
Explanation
Keys will be converted like this: null to '' (empty string), true to 1, false to 0
4.
What gets printed?
class MyException extends Exception {}
try {
throw new MyException('Oops!');
} catch (Exception $e) {
echo "Caught Exception\n";
} catch (MyException $e) {
echo "Caught MyException\n";
}
Correct Answer
A. Caught Exception
Explanation
The first catch will match because MyException is a subclass of Exception, so the second catch is unreachable.
5.
What will be printed?$a = array();if ($a[1]) null;echo count($a), "\n";
Correct Answer
A. 0
Explanation
checking the value in if() does not create an array element.
6.
What gets printed?
$str = 'a\\b\n';
echo $str;
Correct Answer
C. A\b\n
Explanation
\\ is a special case in single-quoted string literals, which gives a single \, \n is not interpolated in single-quoted string literals.
7.
What is the correct way of defining constants in pHP?
Correct Answer
D. Define("Constant");
Explanation
The correct way of defining constants in PHP is by using the define() function. This function takes two parameters: the name of the constant and its value. In this case, the constant is defined as "Constant" without a value, which means it will be an empty constant.
8.
Which function will you use to convert an HTML page into a format that can be saved in the database conveniently?
Correct Answer
B. Htmlentities()
Explanation
The htmlentities() function is used to convert special characters in an HTML page into their corresponding HTML entities. This ensures that the HTML code is properly encoded and can be safely stored in a database without causing any syntax or formatting issues. By using htmlentities(), the HTML page can be converted into a format that can be conveniently saved in the database, preserving the integrity of the original content.
9.
Which of the following functions will be used to create CURL sessions?
Correct Answer
C. Curl_init()
Explanation
The correct answer is curl_init(). This function is used to initialize a new CURL session. It returns a CURL handle, which is then used in other CURL functions to perform various operations like setting options, executing the request, etc. curl_exec() is used to execute the CURL request, curl_opt() is not a valid CURL function, and curl_setopt() is used to set various options for the CURL session.
10.
Which of the following is true about GET & POST methods?
Correct Answer
C. Using POST, you can hide form data from being viewed on address bar or browser
Explanation
The correct answer is that using POST, you can hide form data from being viewed on the address bar or browser. This is because when using the POST method, the data is sent in the body of the HTTP request, rather than being appended to the URL like in the GET method. Therefore, the form data is not visible in the address bar or browser history.
11.
Which of the following statements will be used to fetch SINGLE record from a MySql resultset
Correct Answer
D. Mysql_fetch_row
Explanation
The correct answer is mysql_fetch_row. This function is used to fetch a single row from a MySQL result set as an enumerated array. It returns an array that corresponds to the fetched row, or false if there are no more rows in the result set. This function is commonly used when you only need to retrieve a single record from the result set.
12.
What is the correct syntax for connecting to a MySql database?
Correct Answer
C. Mysql_connect("localhost",$username,$password)
Explanation
The correct syntax for connecting to a MySql database is "mysql_connect("localhost",$username,$password)". This syntax specifies the host as "localhost" and requires the username and password parameters to establish a connection with the MySql database.
13.
Which of the following functions MUST be called AFTER using the mysql_connect() function?
Correct Answer
C. Mysql_select_db
Explanation
The mysql_select_db function must be called after using the mysql_connect function because it is used to select a specific database to work with. After establishing a connection with the database using mysql_connect, it is necessary to select the database that will be queried or manipulated using mysql_select_db. This function allows the user to specify the database name as a parameter, ensuring that all subsequent queries and operations are performed on the selected database.
14.
What gets printed?
<?php
$RESULT = 11 + 011 + 0x11;
echo "$RESULT";
?>
Correct Answer
D. 37
Explanation
A decimal plus an octal plus a hex number. 11 + 9 + 17 = 37
15.
What will be the value of $var below?
$var = true ? '1' : false ? '2' : '3';
Correct Answer
B. 2
Explanation
The value of $var will be 2. This is because the expression is using the ternary operator, which is a shorthand way of writing an if-else statement. In this case, the first condition "true ? '1' : false" evaluates to true, so the value '1' is returned. Then, the second condition "1 ? '2' : '3'" evaluates to true, so the value '2' is returned and assigned to $var.