Jump to content

PHP Test Questions


The Little Guy

Recommended Posts

Thank you for all your feedback It's really helpful, and thanks for spell checking :) I think I got everyone's suggestions and fixed them!

 

Well, I have re-worded many of the questions and replaced the "non-beginner" ones with jesirose's suggestion of "find/fix the error" questions and also fill in the blank.

 

===
The following questions are based on php 5.4.x and lower
===


1. Assuming $var has a value of 0, which statements evaluate to true?
* $var == false
- !isset($var)
- !empty($var)
- $var === false

2. Which of the following does not belong?
- echo "Hello, $first_name";
- echo <<<EOD
	Hello, $first_name
EOD;
* cout << "Hello, $first_name";
- print "Hello, $first_name";

3. Which of the following is a valid variable (not property)?
- var $var = 0;
* $_ = 0;
- $1 = 0;
- $var:int = 0;

4. Which line will throw a syntax error?
<?php
$array = array(1,2,3);
foreach($var in $array){
echo $var;
}
?>
- 1
- 2
* 3
- 4
- 5
- 6


5. Which loop type is intended for arrays?
- do ... while
* foreach
- while
- for

6. What is the correct way to define a function?
- function myFunction(){}
- define function myFunction(){}
- function myFunction():void{}
- $myFunction = function(){};
- Both A and C
* Both A and D

7. What is the incorrect way to define an array?
- $array = array();
- $array = [];
* $array = new Array();
- Both B and C
- All of the above

8. Which of the following methods is meant for variable debugging?
- echo $my_var;
  echo $my_var1;
- echo gettype($my_var);
  echo gettype($my_var1);
* var_dump($my_var, $my_var1);

9. Which of the following is not a valid comment?
* ' This is a comment
- # This is a comment
- /* This is a comment */
- // This is a comment

10. All statements must end with a semi-colon?
* True
- False

11. Blank lines out side of php blocks are fine before printing a header?
- True
* False

12. What is the value of $a on line 10?
<?php
$a = 1;
function add(){
$b = 6;
$a = 5;
$a = $a + $b;
return $a;
}
add();
echo $a;
?>

- 11
- 6
* 1
- 5

13. Fill in the blank:
<?php
______($_GET["year"]){
case 2000:
case 2001:
	echo "2000 or 2001!";
	break;
case 2012:
case 2013:
	echo "2012 or 2013!";
	break;
default:
	echo "Bad Date!";
	break;
}
?>
- if
- define
- while
* switch
- declare

14. What what does a ternary statement look like?
- if($i == 2){
	$color = "red";
  }else{
	$color = "blue"
  }
* $color = $i == 2 ? "red" : "blue";
- if($i == 2)
	$color = "red";
  else
	$color = "blue";

15. What does % (Modulus) do?
- Returns the value after the decimal after division
* Returns the remainder of a value after division
- Just another way to divide two numbers
- Returns the value before the decimal after division

16. What does 3 equal signs mean when comparing values?
* Values must be the same value and same type
- 3 equal signs is an error
- 3 equal signs is the same as 2

17. When php reads functions with functions as parameters it reads:
- From left to right
- From right to left
* From the middle out

18. Comparisons can be passed as parameters to a function?
* True
- False

19. What does "=>" (without the quotes) signify?
- A pointer that points to a method/property in an object
* Assigns a key to its value in an array
- A comparison operator "equal to or greater than"
- Both B and C

20. What does "->" (without the quotes) signify?
* An operator that calls a method/property in an object
- A pointer that points a key to its value from an array
- A comparison operator "greater than zero"
- Both B and C

21. What is the difference between "break" and "continue" within a loop?
- "break" exits a loop and ends execution of the file; "continue" exits a loop and continues file execution
- "break" pauses code execution for a short period; "continue" does not pause code execution
* "break" exits a loop and continues; "continue", continues at the beginning of the loop
- both "break" and "continue" exit a loop and continues the code execution

22. What does "goto" do?
* Allows you to tell php to goto a particular label
- Allows you to tell php to goto a particular line number
- Allows you to tell php to goto a file for execution

23. What is missing?
<?php
$_SESSION["name"] = "Billy Joe";
echo $_SESSION["name"];
?>

- $_SESSION = new Session();
* session_start();
- header("Content-Type: text/html");
- define("$_SESSION", array());

24. Does '"null" == null' evaluate to true or false?
- True
* False

25. How do you escape quotes within quotes?
- echo "<a href=/"http://phpsnips.com/">PHP Snips</a>";
- echo '<a href="http://phpsnips.com">PHP Snips</a>';
* echo "<a href=\"http://phpsnips.com\">PHP Snips</a>";
- echo "<a href="http://phpsnips.com">PHP Snips</a>";

 

Thanks again!

Link to comment
Share on other sites

  • Replies 56
  • Created
  • Last Reply

Top Posters In This Topic

17. When php reads functions with functions as parameters it reads:
- From left to right
- From right to left
* From the middle out

 

I still don't like that one.  It still could be interpreted in either way.  Perhaps a better phrased question would be "In what order are nested function calls evaluated?" with just two options "Inner most first" or "outer most first"  (or you could use left to right/right to left, but drop middle out).

 

I also still don't much care for the wording of #5 or #7.  For #5 perhaps use 'lists' or 'collections' instead of arrays.  For #7 either remove option B or specify in the question PHP v5.4 (or drop the 'and lower' part from the beginning of your quiz).

 

 

Link to comment
Share on other sites

I like the new ones you added.

 

Thank you!

 

New #17:

17. In what order are nested function calls evaluated?
- Outer most function first
* Inner most function first

 

I dropped the "and lower" its php 5.4.x now

 

 

How about this (I don't quite like "lists" or "collections"?

5. Which loop type is best used for arrays?
- do ... while
* foreach
- while
- for

Link to comment
Share on other sites

23. What is missing?

<?php

$_SESSION["name"] = "Billy Joe";

echo $_SESSION["name"];

?>

 

- $_SESSION = new Session();

* session_start();

- header("Content-Type: text/html");

- define("$_SESSION", array());

 

I don't really like that. The code will run and work fine as it stands, except it won't be a real session. Plus, it's not immediately obvious that this snippet of code is actually the start of an application.

 

Maybe I'm just nitpicking now.

Link to comment
Share on other sites

23. What is missing?

<?php

$_SESSION["name"] = "Billy Joe";

echo $_SESSION["name"];

?>

 

- $_SESSION = new Session();

* session_start();

- header("Content-Type: text/html");

- define("$_SESSION", array());

 

I don't really like that. The code will run and work fine as it stands, except it won't be a real session. Plus, it's not immediately obvious that this snippet of code is actually the start of an application.

 

Maybe I'm just nitpicking now.

 

I think it's a fair crit.  Given how easy (and ubiquitous) it is to enter/exit PHP, it's hard to tell if that snippet is an entire script or if we're seeing code that's in medias res within a larger script.

Link to comment
Share on other sites

Q: What is the visibility of this class function: function set_name(){}

    * public

Q: Who / What can access private properties or methods?

    * only the class itself

Q: Who / What can access protected properties or methods?

    * Class itself, parents, children

Q: Who / What can access public properties or methods?

    * Everybody :yay:

Link to comment
Share on other sites

I have testing up for the "Beginner" test, so if you wanna give it a try, here is the link:

http://phpsnips.com/test.php

 

Eventually it will require a login, so scores can be saved to a profile, but none of that is in place atm, just the test and the percent correct at the end.

 

@Mahngiel, Thanks for the questions, I will probably use those in the "Advanced" Test.

Link to comment
Share on other sites

Q: Who / What can access protected properties or methods?

    * Class itself, parents, children

 

This should just be "Class itself and its children."

 

http://php.net/manual/en/language.oop5.visibility.php

Members declared protected can be accessed only within the class itself and by inherited and parent classes

 

Huh.  That's odd, as generally one wouldn't ever need to have a parent access a child's protected member (okay, that sounds dirty).  The whole point of protected is to say "From this point on in the class hierarchy, only me and my children can see this."

Link to comment
Share on other sites

Q: Who / What can access protected properties or methods?

    * Class itself, parents, children

 

This should just be "Class itself and its children."

 

http://php.net/manual/en/language.oop5.visibility.php

Members declared protected can be accessed only within the class itself and by inherited and parent classes

 

Huh.  That's odd, as generally one wouldn't ever need to have a parent access a child's protected member (okay, that sounds dirty).  The whole point of protected is to say "From this point on in the class hierarchy, only me and my children can see this."

 

Yeah, that sounds literally ass-backwards.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.