Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

PHP Fundamentals, Exams of Computer Science

A variety of php-related topics, including magic constants, error handling, file uploads, array manipulation, and more. It provides answers to multiple-choice questions on these subjects, which can be useful for students studying php or preparing for exams. A broad range of php concepts and functions, making it a potentially valuable resource for university-level php courses. The questions and answers touch on topics such as session management, smtp email, array operations, and file handling, providing a comprehensive overview of essential php programming knowledge.

Typology: Exams

2023/2024

Available from 08/26/2024

dennis-mburu
dennis-mburu 🇺🇸

85 documents

1 / 78

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PHP Test
1. What is the best practice for running MySQL queries in PHP?
Consider the risk of SQL injection.
Answers:
1. Use mysql_query() and variables: for example: $input =
$_POST[‘user_input’]; mysql_query(“INSERT INTO table (column)
VALUES (‘” . $input . “‘)”);
2. Use PDO prepared statements and parameterized queries: for
example: $input= $_POST[“user-input”] $stmt = $pdo-
>prepare(‘INSERT INTO table (column) VALUES (“:input”); $stmt-
>execute(array(‘:input’ => $input));
3. Use mysql_query() and string escaped variables: for example:
$input= $_POST[“user-input”] $input_safe =
mysql_real_escape_string($input); mysql_query(“INSERT INTO
table (column) VALUES (‘” . $input. “‘)”);
4. Use mysql_query() and variables with a blacklisting check: for
example: $blacklist = array(“DROP”,”INSERT”,”DELETE”);
$input= $_POST[“user-input”] if (!$array_search($blacklist)))
mysql_query(“INSERT INTO table (column) VALUES (‘” . $input.
“‘)”);
2. Which of the following methods should be used for sending
an email using the variables $to, $subject, and $body?
Answers:
1. mail($to,$subject,$body)
2. sendmail($to,$subject,$body)
3. mail(to,subject,body)
4. sendmail(to,subject,body)
3. Which of the following is used to maintain the value of a
variable over different pages?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e

Partial preview of the text

Download PHP Fundamentals and more Exams Computer Science in PDF only on Docsity!

PHP Test

1. What is the best practice for running MySQL queries in PHP? Consider the risk of SQL injection. Answers: 1. Use mysql_query() and variables: for example: $input = $_POST[‘user_input’]; mysql_query(“INSERT INTO table (column) VALUES (‘”. $input. “‘)”); 2. Use PDO prepared statements and parameterized queries: for example: $input= $_POST[“user-input”] $stmt = $pdo- >prepare(‘INSERT INTO table (column) VALUES (“:input”); $stmt- >execute(array(‘:input’ => $input)); 3. Use mysql_query() and string escaped variables: for example: $input= $_POST[“user-input”] $input_safe = mysql_real_escape_string($input); mysql_query(“INSERT INTO table (column) VALUES (‘”. $input. “‘)”); 4. Use mysql_query() and variables with a blacklisting check: for example: $blacklist = array(“DROP”,”INSERT”,”DELETE”); $input= $_POST[“user-input”] if (!$array_search($blacklist))) mysql_query(“INSERT INTO table (column) VALUES (‘”. $input. “‘)”); 2. Which of the following methods should be used for sending an email using the variables $to, $subject, and $body? Answers: 1. mail($to,$subject,$body) 2. sendmail($to,$subject,$body) 3. mail(to,subject,body) 4. sendmail(to,subject,body) 3. Which of the following is used to maintain the value of a variable over different pages?

Answers:

  1. static
  2. global
  3. session_register()
  4. None of these 4. Which of the following will check if a function exists? Answers:
  5. function_exists()
  6. has_function()
  7. $a = “function to check”; if ($a ()) // then function exists
  8. None of these 5. Which of the following is not a file-related function in PHP? Answers:
  9. fclose
  10. fopen
  11. fwrite
  12. fgets
  13. fappend 6.Which of the following is true about the singleton design pattern? Answers:
  14. A singleton pattern means that a class will only have a single method.
  1. private $type = ‘moderate’;
  2. internal $term = 3;
  3. public $amnt = ‘500’;
  4. protected $name = ‘Quantas Private Limited’; 10. Which of the following is not a PHP magic constant? Answers:
  5. FUNCTION
  6. TIME
  7. FILE
  8. NAMESPACE
  9. CLASS 11. Which of the following will print out the PHP call stack? Answers:
  10. $e = new Exception; var_dump($e->debug());
  11. $e = new Exception; var_dump($e->getTraceAsString());
  12. $e = new Exception; var_dump($e->backtrace());
  13. $e = new Exception; var_dump($e->getString()); **12. What will be the output of the following code?
Answers:** 1. int(3*4)
  1. int(12)
  2. 3*
  3. 12
  4. None of the above 13. Which of the following is correct about Mysqli and PDO? Answers:
  5. Mysqli provides the procedural way to access the database while PDO provides the object oriented way.
  6. Mysqli can only be used to access MySQL database while PDO can be used to access any DBMS.
  7. MySQLi prevents SQL Injection whereas PDO does not.
  8. MySQLi is used to create prepared statements whereas PDO is not. 14. What is the correct way to send a SMTP (Simple Mail Transfer Protocol) email using PHP? Answers:
  9. s.sendmail($EmailAddress, [$MessageBody], msg.as_string())
  10. sendmail($EmailAddress, “Subject”, $MessageBody);
  11. mail($EmailAddress, “Subject”, $MessageBody);
  12. $MessageBody 15. Which of the following will start a session? Answers:
  13. session(start);
  14. session();
  1. application/octect-stream
  2. None of these 18. Which of the following is incorrect with respect to separating PHP code and HTML? Answers:
  3. Use an MVC design pattern.
  4. As PHP is a scripting language, HTML and PHP cannot be separated.
  5. Use any PHP template engine e.g: smarty to keep the presentation separate from business logic.
  6. Create one script containing your (PHP) logic outputting XML and one script produce the XSL to translate the XML to views. 19. Which one of the following is not an encryption method in PHP? Answers:
  7. crypt()
  8. md5()
  9. sha1()
  10. bcrypt() 20. What function should you use to join array elements with a glue string? Answers:
  11. join_st
  12. implode
  13. connect
  1. make_array
  2. None of these 21. Which function can be used to delete a file? Answers:
  3. delete()
  4. delete_file()
  5. unlink()
  6. fdelete()
  7. file_unlink() 22. What is the string concatenation operator in PHP? Answers:
  8. || 3..
  9. |||
  10. None of these 23.Which of the following is useful for method overloading? Answers:
  11. __call,__get,__set
  12. _get,_set,_load
  13. __get,__set,__load
  14. __overload

27. What is the best way to change the key without changing the value of a PHP array element? Answers: 1. $arr[$newkey] = $oldkey; unset($arr[$oldkey]); 2. $arr[$newkey] = $arr[$oldkey]; unset($arr[$oldkey]); 3. $newkey = $arr[$oldkey]; unset($arr[$oldkey]); 4. $arr[$newkey] = $oldkey.GetValue(); unset($arr[$oldkey]); 28. What will be the output of the following code? <? echo 5 * 6 / 2 + 2 * 3; ?> Answers: 1. 1 2. 20 3. 21 4. 23 5. 34 29. Does PHP 5 support exceptions? Answers: 1. Yes 2. No 30.Which of the following is true about posting data using cURL in PHP? Answers:

  1. Data can be posted using only the POST method.
  2. Data can be posted using only the GET method.
  3. Data can be posted using both GET and POST methods.
  4. Data cannot be posted using cURL. 31.Which of the following is the correct way to check if a session has already been started? Answers:
  5. if (session_id()) echo ‘session started’;
  6. if ($_SESSION[«session_id»]) echo ‘session started’;
  7. if ($GLOBALS[«session_id»]) echo ‘session started’;
  8. if ($_SERVER[«session_id»]) echo ‘session started’; 32.Given the following array: $array = array(0 => ‘blue’, 1 => ‘red’, 2 => ‘green’, 3 => ‘red’); Which one of the following will print 2? Answers:
  9. echo array_search(‘green’, $array);
  10. echo array_key_exists(2, $array);
  11. echo in_array(‘green’, $array);
  12. echo array_search(‘red’,$array); 33.What is the output of the following code? echo 0x500; ?> Answers:
  13. 500
  1. PHP does not have any built-in function that can do this: the source array will have to be copied, and $item inserted in to the required position: $n = 0; foreach ($array as $key => $val) { if ($n == $position) { $target[] = $item; } ++$n; $target[$key] = $val; }
  2. array_insert() 37.Consider the following 2D array in PHP: $array = array(array(141,151,161), 2, 3, array(101, 202, 303)); Which of the following will display all values in the array? Answers:
  3. function DisplayArray($array) { foreach ($array as $value) { if (array_valid($value)) { DisplayArray($value); } else { echo $value. “ ”; } } } DisplayArray($array);
  4. function DisplayArray($array) { for ($array as $value) { if (valid_array($value)) { DisplayArray($value); } else { echo $value. “ ”; } } } DisplayArray($array);
  1. function DisplayArray($array) { for ($array as $value) { if (is_array($value)) { DisplayArray($value); } else { echo $value. “ ”; } } } DisplayArray($array);
  2. function DisplayArray($array) { foreach ($array as $value) { if (is_array($value)) { DisplayArray($value); } else { echo $value “ ”; } } } DisplayArray($array); 38. Why is it not recommended to use $_REQUEST when handling form submissions in PHP? Answers:
  3. It’s difficult to determine whether it is a $_POST or $_GET request.
  4. $_REQUEST is deprecated
  5. $_REQUEST includes $_COOKIE by default, and parameters from $_COOKIE with the same name may be overriden with parameters from $_GET or $_POST.
  6. $_REQUEST does not handle HTTP rquests, it handles database requests. 39.How should a variable be declared in a function, if the value has to be retained over multiple calls? Answers:

42.Which of the following file modes is used to write into a file at the end of the existing content, and create the file if the file does not exist? Answers:

  1. r+
  2. w+
  3. a()
  4. x 43.Without introducing a non-class member variable, which of the following can be used to keep an eye on the existing number of objects of a given class? Answers:
  5. Adding a member variable that gets incremented in the default constructor and decremented in the destructor.
  6. This cannot be accomplished since the creation of objects is being done dynamically via «new.»
  7. Add a static member variable that gets incremented in each constructor and decremented in the destructor.
  8. Adding a local variable that gets incremented in each constructor and decremented in the destructor. 44.What is the output of the following code?
echo xyz(); ?> Answers:

  1. abc
  2. FUNCTION
  3. xyz

45.Which of the following is the operator with the highest precedence? Answers:

  1. instanceof
  2. new
  3. =
  4. none of above 46.Which function is used to remove the first element of an array? Answers:
  5. array_remove_first_element
  6. array_shift
  7. array_ltrim
  8. a[0] = nil
  9. None of these

vec_add (&$a, $b) { $a[‘x’] += $b[‘x’]; $a[‘y’] += $b[‘y’]; $a[‘z’] += $b[‘z’]; } $a = array (x => 3, y => 2, z => 5); $b = array (x => 9, y => 3, z => -7); vec_add (&$a, $b); print_r ($a); ?> Answers:

  1. Array ( [x] => 9 [y] => 3 [z] => - )
  2. Array ( [x] => 3 [y] => 2 [z] => 5 )
  3. Array ( [x] => 12 [y] => 5 [z] => - )
  4. Error
  5. None of these 51.Which of the following are not considered as Boolean false? Answers:
  6. FALSE

4. «FALSE»

  1. null 52.Which of the following code snippets has the most appropriate headers to force the browser to download a CSV file? Answers:
  2. header(«Content-type: text/csv»); header(«Content-Disposition: attachment; filename=file.csv»); header(«Pragma: no-cache»); header(«Expires: 0»);
  3. header(‘Content-Type: application/download’); header(«Content-Disposition: attachment; filename=file.csv»); header(«Pragma: no-cache»); header(«Expires: 0»);
  4. header(‘Content-Type: application/csv’); header(«Content-Disposition: attachment; filename=file.csv»); header(«Pragma: no-cache»); header(«Expires: 0»);
  5. header(‘Content-Type: application/octet-stream’); header(«Content-Disposition: attachment; filename=file.csv»); header(«Pragma: no-cache»); header(«Expires: 0»); 53. What is the output of the following code?
»; $array = array(«red»,»green»,»blue»); $last_key = end(array_keys($array));