Jump to content

PHP Test Questions


The Little Guy

Recommended Posts

I am going to be making tests for my site, right now I am building a list of questions. Does any have any input on some good questions to add?

 

There will be 3 (maybe 4) PHP tests:

- Beginner (structure / construct)

- Intermediate (functions / classes)

- Advanced (not sure atm)

- Expert (Maybe)

 

So, if anyone would like to add some questions, please let me know!

Link to comment
Share on other sites

  • Replies 56
  • Created
  • Last Reply

Top Posters In This Topic

What is the difference between == and ===?

 

Assuming $var has a value of 1, which statements evaluate to true?

[*]$var == true

[*]isset($var)

[*]empty($var)

[*]$var === true

 

Assuming $var has a value of 0, which statements evaluate to true?

[*]$var == false

[*]isset($var)

[*]empty($var)

[*]$var === false

Link to comment
Share on other sites

Thanks for some of the questions, I have used a few of them! Here is one I added, not sure if it is too mean (trick question)...

 

* = correct answer

All lines MUST end with a semi-colon?

- True

* False

 

The wording could be better.  'Lines' is vague.  Do you mean to include comments?  Opening/closing PHP tags?

Link to comment
Share on other sites

That doesn't really make sense. The obvious answer would be that no, not every line would end in a semi colon. People will get this wrong simply because they will think you mean something else.

 

Agreed.  Remember, questions of this caliber are aimed at newbies.  Don't ask vague questions, or what you may think are clever trick questions.  The whole point is for them to test their knowledge and further their understanding, not engage in trivial pursuit.

Link to comment
Share on other sites

the ending ?> automatically terminates the line in the second one.

 

And how would that be useful to a beginner?

 

Really, a question like this does more to prove you know about the details of the language than help a newbie reaffirm they have a grasp of the basics.

 

I agree.

Link to comment
Share on other sites

Okay, so I came up with 25 questions for the beginners test. Any suggestions on any of these? I would like to try an keep the test 25 - 30 questions long. Thanks for the input!

 

===
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?
- var $var = 0;
* $_ = 0;
- $1 = 0;
- $var:int = 0;

4. What is the output of the following code:

for($i = 0; var_dump(100), $i < 10; $i++);

- An error
- prints "100" 10 times
- Nothing
* prints "int(100)" 10 times

5. Which loop type is souly 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 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. What is the final output of this code?
<?php
$a = "hello";
$$a = "world";
echo "$a $hello";
?>
- hello $hello
* hello world
- An error ($hello is not defined)
- An error (You can not have $$ as a variable)

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 ofa 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
* A pointer that points a key to its value from an array
- A comparison operator "eqal to or greater than"
- Both B and C

20. What does "->" (without the quotes) signiy?
* A pointer that points to 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 continue 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 the output on line 8?
<?php
function foo($x){
return function($y) use ($x){
	return $y + $x;
};
}
$a1 = foo(10);
echo $a1(2);
?>

- 2
- 10
* 12
- 0

24. What is an Anonymous function?
* A function that has no name
- A function that is sometimes a function
- A function that is also a vaiable
- A function who's name is anonymous (function anonymous(){})

25. How do you escape quotes withing 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>";

Link to comment
Share on other sites

#4 I have never seen someone use that syntax, and there is little if anything in the manual that indicates how it can be used. Not beginner material. See Kevin's point above.

#5 - solely not souly.

#10 - see above.

#13 - also not beginner material.

#15 - maybe include the % not just the name.

#16 - see 15

17 - I don't even understand what you're asking, and if I do understand it's not even a sensible question.

19 - equal

23&24 - not beginner material

 

What would be useful is "find the error" questions (show code and ask to find lines with errors, without giving the PHP generated error), as well as "fix the error" (given a syntax error, fix the code). Include logical errors not just code errors.

Link to comment
Share on other sites

This:

 

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

 

Is wrong.  Isset will evaluate to true since 0 !== null.  Empty will evaluate to true since 0 is considered the same as empty. 

 

Also, neither -> nor => are pointers.  At all.  -> is the to member operator.  => is essentially an indexer.  Pointer has a very specific meaning, one which is never used in PHP itself since we cannot ever touch dynamic memory. 

Link to comment
Share on other sites

I could see #3.1 being argued as correct as it is the definition of a class property (which arguably is a variable in its own.)

#11 probably needs clarification, like "before printing a header"

#15.2 needs a space between "of" and "a"

#21.3 needs clarification on what it continues IMO

Is 23/24 really a beginners question?

 

Plus all of the above comments ;)

Link to comment
Share on other sites

(others beat me to some of these but here ya go anyway)

 

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

 

empty($var) would be true as well.

 

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

 

var $var=0; is technically valid, depending on how picky you want to get:

class Blah {
    var $var = 0;
    public function Blah(){ var_dump($this->var); }
}

 

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

 

No true.  You can use foreach on objects, and it is intended as well (See the Iterator interface);

 

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

D should be $myFunction = function(){};

 

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

This depends on what version your looking at.  $array=[]; is valid in php 5.4 only (your "5.4.x and lower" implies covering 5.3 and 5.2).  There are still plenty of people not using that version.

 

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

 

This could be mis-interpreted.  I assuming you mean that if you do something like:

func(anotherfunc($a, $b));

php will first execute anotherfunc, then func.

 

An alternate meaning for that question though would be given this line:

func($a, $a++, $b, $c+30);

in what order are the parameters run?  The answer to that is left-to-right.

 

[code

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 continue code execution

[/code]

I would change the wording on that answer so you don't use 'continue' so much.  Perhaps something like ""break" exists the loop and continues script execution; "continue" jumps immediately to the next iteration of the loop."

 

-----

 

You have various spelling errors in some questions too, so do a proof-read.

 

Link to comment
Share on other sites

3. Which of the following is a valid variable?

- var $var = 0;

* $_ = 0;

 

Technically, var $var = 0; is valid for class variables.

 

5. Which loop type is souly intended for arrays?

* foreach

 

Foreach also works with objects.

 

6. What is the correct way to define a function?

- myFunction = function(){};

 

Ehhhhhhh. I don't know about this one. It's not strictly about defining a function. But, I digress.

 

7. What is the incorrect way to define an array?

- $array = [];

 

This is only valid for PHP >= 5.4, so it might confuse some beginners.

 

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

 

I'm not sure var_dump() is meant for debugging. It is helpful, sure. echo's can be just as helpful, depending on the situation. Other things like print_r or var_export() can be useful as well.

 

21. What is the difference between "break" and "continue" within a loop?

* break exits a loop and continues; continue, continues at the beginning of the loop

 

This is a little vague if not inaccurate. break exits from a loop at the current iteration and proceeds through the rest of the code. continue breaks from a loop at the current iteration, and begins the next iteration.

 

EDIT: Damn, kicken beat me on a few of these.

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.