SOEN 287

  1. Q: Can you override in JS? In PHP?
    A: Yes, No
  2. Q: What are the main differences/similarities between Java and JavaScript?
    • A: Java and JS are only related through syntax. - JS is dynamically typed
    • - Objects are treated very differently
  3. Q: State and advantage of JS computations being done on the client-side
    A: Transfers load from often over-loaded servers to under-loaded clients
  4. Q: Name the two types of embedding for JS, and main pros/cons of each.
    • A:
    • Implicit
    • - external file by adding 'src' attribute
    • - Good because it hides script from users and older incompatible browsers
    • - Good because it seperates two different notations

    • Explicit
    • - directly embedded in script
    • - Bad as it mixes notations
    • - Bad as it is difficult for different 'expert' to work on same doc
    • - Can be convenient in certain cases (like very little JS)
  5. Q: How is JavaScript not technically object-oriented?
    • A: - It does not support inheritance or polymorphism. It is considered more of an 'object-based language'.
    • - Objects in JS are descendent from the root Object, and are a collection of data properties
  6. Q: How would you write a script in HTML in a way to hide it from incompatible browsers?
    A: <script> <!-- JavaScript code //--></script>
  7. Q: Explain the "somewhat optional" nature of semi-colons.
    A: JS tries to implicitly insert them where they make sense, but sometimes misses and cuts statements off at points where they could be standalone expressions, but really should include the rest of the line. Thus best to specify them yourself and to place statements all on one line when possible.
  8. Q: Name the Primitive Types and Wrapper Objects in Java.
    • A: Primitives: Number, Boolen, String, Undefined, Null
    • Wrappers: Number, Boolean, String
  9. Q: What is the difference between the use of single (') vs double (") quotes in JS?
    A: None, just be consistent. Also, when inserting the same type of quote within the string, it needs to be escaped. In other words, you need to escape singles within a single, and doubles within a double, but not a double within a single and vice versa..
  10. Q: How do you check for NaN?
    A: isNaN()
  11. Q: Describe how Boolean objects are interpreted in JS.
    • A: String - TRUE, unless it is EMPTY or ZERO ("0"(but not "0.0"))
    • Number - TRUE, unless it is ZERO
    • NaN - FALSE
    • Undefined - FALSE

    • Relational expressions:
    • String and Number - Attempts to coerce string to number
    • Bool and NonBool - Coerces boolean to number; 0 or 1

    • Conditional expression:
    • If a boolean is used in a conditional expression: - It is TRUE, unless it is NULL or UNDEFINED
  12. Q: List what the typeof operator returns for each type.
    • Number: "number"
    • String: "string"
    • Boolean: "boolean"
    • Object or Null: "null"
    • Undefined: "undefined"
    • Function: "function"
  13. Q: What precedences exist when JavaScript tries to coerce variables of mismatching types in an operation?
    A: It will go by the lest variable first. So if the left variable is a string, the operator '+' and the righ a number, it will corece the right to a string and concatenate. However if the orders are reversed, it will try to add first.
  14. Q: What are the two ways arrays can be created in JS?
    • A: 1. Use Array() contructor
    • var arr1 = new Array(1, 2, “three”, “four”);
    • // Length 4 and initialized to values

    • var arr2 = new Array(100);
    • // Length 100, uninitialized

    • 2. Use literal array values var arr3 = [1, 2, “three”, “four”];
    • // Equivalent to arr1
  15. Q: What methods allow you to make an array into a string, and a string into an array?
    A: join() will combine the elements of an array into a string. Seperator is comma by default, or whatever param is passed

    split() will split a string into an array. Seperator is space by default, or whatever param is passed
  16. Q: How do you sort an array in JS? Take arr1 as an example.
    A: arr1.sort(); // Coerces elements to strings and sort lexicographically
  17. Q: Difference between pop(), push() and shift(), unshift()?
    A: pop and push deal with end of array, shift and unshift with beginning
  18. Q: How do you add elements to the end of an array in JS?
    A: the concat() method. Pass the value to add as an argument
  19. Q: Why are functions generally placed in the head of an HTML document?
    A: They should be defined before they are used. This way they are defined before any calls from the body.
  20. Q: Describe scope in JS.
    A: Any variable declared outside of functions has global scope; there is not such thing as block-scope. An array defined within a function definition has local-scope, and takes precedence of any global variableof the same name.
  21. Q: How do you use the arguments array?
    A: Arguments passed are placed in an implicit arguments array, where they can be accessed like in any other array. It is particularly useful when dealing with a variable number of parameters (can use arg-array in a loop or something).
  22. Q: What are the 4 main methods used in RegEx matching in JS, and what are they for?
    • search() - returns index of matched pattern
    • match() - returns matches in an array
    • replace(pat,rep) - replaces pattern (pat) with replacement (rep)
    • split(sep) - seperates string into an array at seperator (sep)
  23. Q: What are parantheses used for in RegEx matching? How might their purpose be altered?
    A: The pattern in parantheses is remembered. Can be "forgotten" by including '?:' e.g. (?:x) will match x but not store it in matches array
  24. Q: List the nine shorthands in RegEx.
    • '.' Matches any characeter except newline
    • [ ... ] Can define classes of characters to search
    • [^ ... ] Inverses the specified set, so everying “except” these.
    • \d or [0-9] A digit
    • \D or [^0-9] Not a digit
    • \w or [A-Za-z_0-9] An alphanumeric word character
    • \W or [^A-Za-z_0-9] Not a word character
    • \s or [ \r\t\n\f] A white space character
    • \S or [^ \r\t\n\f] Not a white space character
  25. Q: What are braces ({}) used for in RegEx? Describe the 3 ways they can be used.
    • A: To specify how many repitions of the pattern should be found.
    • - {n}: strictly n reptitions
    • - {n,m}: At least n, but no more than m repitions
    • - {n,}: At least n repitions
  26. Q: Explain the difference between *, ? and + in RegEx.
    • * - zero or more
    • + - one or more
    • ? - one or none
  27. Q: What does the placement of '^' change?
    A: In []s, inverses the pattern. Outside [] means pattern must be first thing in search string.
  28. Q: What are the three main modifiers in RegEx and their meanings?
    • i - ignore case
    • x - ignore whitespace
    • g - global, meaning search entre string for all matches, not just one
  29. Q: Describe the sneaky for-loop that lets you cycle all elements in an array; take arr1 for example.
    • for(var prop in arr1){
    • document.write(arr1[prop] + "<br />");
    • }
  30. Q: What about the length property in JS is particular compared to Java?
    A: It can be used to SET lenth. If length is reset, undefined elements have value "undefined", and elements no longer in length are deleted (i think).
  31. Q: Are number of params passed through functions in JS checked?
    A: NO. Unused formal parameters are unbound, and excess actual parameters are ignored.
  32. Q: What is DOM? Describe 3 of its features.
    • - The DOM is an abstract model that defines the interface between HTML documents and application programs—an API
    • - Documents in the DOM have a treelike structure
    • - A language that supports the DOM must have a binding to the DOM constructs
  33. Q: How can you access forms and elements using DOM?
    A: document.form[i].elements[j]; // Forms and Elements are placed into implicit arrays (properties of document object) based on their order
  34. Q: What is the problem with using 'name' attributes to call say a form in JS? What is a better solution
    A: Name attribute not accepted in xhtml. Best to use document.getElementById('id');
  35. Q: Define 'event', 'event handler' and 'registration'.
    • - An event is an object (notification) created by the browser to notify that something specific has occured.
    • - An event handler is a script that is implicitly executed in response to the appearance of an event
    • - The process of connecting an event handler to an event is called registration
  36. Q: What are the two ways an event handler can be registered?
    Method 1. Assign event handler to attribute By assigning the event handler script to an event tag attribute

    • Method 2. Assign event handler to property
    • - Assign the address of the handler function to the event property of the JavaScript object associated with the HTML element.
    • - The registration must follow both the handler function and the HTML form
  37. Q: Why is doing a certain amount of form-validation on the client-side useful?
    A: Saves sever some time and effort. It is often over-loaded whilst client is not.
  38. Q: What are the two 'modes' of PHP?
    • A:
    • Copy – when it encounters markup code, it simply copies it to the output file as is

    Interpret – when it encounters php script, it interprets it and send any output to the output file. Thus the output should be in HTML.
  39. Q: What are the 8 prim-types in PHP?
    A: Interger, Double, String, Boolean, array, object, null, resource
  40. Q: What is the difference between single (') and double (") quotes in PHP?
    A: When using singles, variables and excape sequences are not interpolated.
  41. Q: Describe what happens when non-boolean variables are interpreted in a boolean context in PHP.
    Integer: FALSE if ZERO, TRUE otherwise

    String: FALSE if “” or “0”, TRUE otherwise (including “0.0”)

    Double: Only 0.0 exactly evaluates to FALSE (again, this isn't the same as the string “0.0”, which evaluates to TRUE.
  42. Q: How are strings coerced to numbers in PHP?
    A: Strings are coerced to numbers if they begin with a number, otherwise result in zero. If the initial number is a double (ie contains a decimal point or 'e' notation, it can be coerced as a double). Non-numeric characters following the number are ignored.
  43. Q: What are the 3 ways variables can be explicitly converted in PHP?
    • Explicit type conversion can be done using C-like notation: (int) $sum;
    • or calling one of the intval, doubleval or strval function: intval(sum);
    • or also by using the settype function: settype($sum, “integer”);
  44. Q: What is particular about arrays in PHP?
    A: Essentially a combination of all sorts of arrays. Stored as key-value pairs, so can behave like traditional array, or hash-list
  45. Q: What are the 2 ways arrays can be declared in PHP?
    • 1. Assigning a value to a subscripted variable that was previously not an array creates the array: Ex: $list[0] = 17;
    • // $list is not an array with one element: 0 => 17

    If empty brackets are furnished, a key is implicitly added. This will be one greater than the previous elements key if it is a number, or '0' if no numeric key has been previously used in the array. Furthermore, elements of an array need not be of the same type.

    • 2. Using the array contruct:
    • Ex: $list2 = array(17, 24, 45, 91);
    • // 0 =>17, 1 => 24, 2 => 45, 3 => 91

    • $list3 = array(1 =>17, 2 => 24, 3 => 45, “woah” => 91);
    • // Can specify keys
  46. Q: How are arrays and string converted between each other in PHP?
    A: explode(" ", $arr) and implode(" ", $arr) // Seperator a space here, but can be specified to whatever
  47. Q: How does the foreach loop work in PHP?
    • A: foreach($array as $value)
    • or
    • foreach($array as $key => $value)
  48. Q: List and explain all the sort functions we saw in PHP.
    • sort($array) - Sorts values and replaces keys with 0,1,2...
    • asort($array) - Sort values, but keeps original keys
    • ksort($array) - Sorts by keys rather than values
    • rsort, arsort and krsort do the same, but in reverse.
  49. Q: List the general characteristics of functions in PHP.
    • Functions need not appear in a document before they are called, so their placement is somewhat irrelevant.
    • Functions can not be overloaded.
    • Functions are not case sensitive.
    • Much like in JS, number of params is not checked; excess is ignored, too few means remaining formal are unbound
  50. Q: How do you pass-by-reference in PHP?
    A: Add an ampersand (&) before the variable passed in the function.
  51. Q: Describe the scope of variables in PHP.
    A: The default scope of functions is local, thus hiding any variables of the same name outside of the function. If you want the scope of a variable to be global, you must add this declaration outside of the function.
  52. Q: What are the two main functions we use for pattern-matching in PHP?
    preg_match(pattern_to_match, $str_to_match); // returns boolean and

    preg_split(pattern_to_match, $str_to_match); // returns array of split string
  53. Q: How are forms handled in PHP?
    A: Depending on method (POST or GET), all form elements will be stored in implicit array ($_POST or $_GET) by their name, and their values can be accessed from there.
  54. Q: List security features of Cookies.
    • - Cookies are returned only to the server that created them
    • - Cookies can be used to determine usage patterns that might not otherwise be ascertained by a server
    • - Browsers generally allow users to limit how cookies are used
    • - Browsers usually allow users to remove all cookies currently stored by the browser
    • - Systems that depend on cookies will fail if the browser refuses to store them
    • - Not all clients support or accept cookies
    • - Cookies can be disabled
    • - This is similar to the client-side validation using JavaScript
    • - According to the cookie specification, no cookie can exceed 4 KB in size,
    • - Only 20 cookies are allowed per domain
    • - A total of 300 cookies can be stored on the client side
  55. Q: What are the main differences between sessions and cookies?
    A cookie is one means of tracking customer data, which can be useful for a variety of reasons, like cart-tracking or personlization of the webpage. The main problem with cookies is that they can be blocked or disabled by the user, making them useless. Otherwise, they are sent to the server where they can be processed and used to send back specific information to the client.

    A session is the time span during which a browser interacts with particular server. A session begins when a browser connects to the server and ends when either the browser is closed or the server terminates the session due to inactivity (usually default is something like 30min).

    • Similarities with cookies:
    • - Hold information about one single user
    • - Pass information from one page to another in your application

    • Differences from cookies:
    • - Store the information at the server side instead of the client side
    • - Session information is deleted after the user has left the website (although permanent storage is possible)
  56. Q: How is a session started and tracked?
    • For session tracking, PHP creates and maintains a session tracking id
    • - The session id is either stored in a cookie or is propagated in the URL
    • - Create the id with a call to session_start() with no parameters
    • - To use cookie-based sessions, session_start() must be called before outputting anything to the browser
    • - To store and retrieve session variables, use the PHP $_SESSION variable
  57. Q: How do you destroy a sesion?
    • Delete some session data using unset( ) function
    • <?php
    • unset($_SESSION['views']);
    • ?>

    • Completely destroy the session by calling the session_destroy() function
    • <?php
    • session_destroy();
    • ?>

    • - session_destroy() destroys all of the data associated with the current session
    • - It does not unset any of the global variables associated with the session, or unset the session cookie.
    • - To use the session variables again, session_start() has to be called
Card Set
SOEN 287
SOEN 287 Test Review