Ruby interview questions

Ruby quiz questions

  • 1.
    rapper = "Jaydakiss"
    

    Get the first letter from the string "Jaydakiss".

    Answer:

    "Jaydakiss"[0]
    # OR
    rapper[0]
    

    Basically everything in computer science is 0-indexed, so the counting starts at zero, not at 1.

    View
  • 2.

    What does this expression print?

    my_name = "50 Cent"
    my_name = "Miley"
    p my_name

     

    Answer:

    "Miley"
    

    The my_name variable was assigned to the value "50 Cent", but then it was reassigned to the value "Miley". Once the my_name variable is reassigned to "Miley", it loses all knowledge that it used to point to "50 Cent".

    View
  • 3.
    a = "Bat"
    b = "woman"
    

    What does this expression evaluate to?

    a + b

    Answer:

    "Batwoman"
    

    When variables are assigned to values, the variable can be used in place of the value.

    View
  • 4.

    What does this expression evaluate to?

    "bob" + 42

    Answer:

    This raises an error because a number (Integer to be precise) cannot be concatenated with a String.

    View
  • 5.

    What does this expression evaluate to?

    "Cool".+("io")

    Answer:

    "Coolio"
    

    This is another way to concatenate Strings. The previous example actually uses syntactic sugar (a deviation from formal syntax to enhance code readability) and this example uses the strict syntax.

    View
  • 6.

    What does this expression evaluate to?

    "YO" + "LO"

    Answer:

    "YOLO" - This is called string concatenation.

    View
  • 7.
    x = 5
    

    Is x a string?

    Answer:

    x is a variable and is not a string. x is assigned to the value 5 and 5 is an Integer, not a String. Notice that variables are not surrounded with quotation marks.

    View
  • 8.

    What is the datatype of "Hello"?

    Answer:

    "Hello" is a String. Strings are text that are surrounded by double or single quotes ("Hello" and 'Hello' are acceptable syntax).

    View

© 2017 QuizBucket.org