Git Product home page Git Product logo

ruby-oo-fundamentals-instance-variables-and-scope-quiz's Introduction

Quiz: Variables and Scope in Ruby

Introduction

Welcome to our quiz! Quizzes are designed for knowledge exploration and finding weaknesses in your understanding. Use this to check up on your understanding. Even if you see the "right" answer, consider what it would take to change the given code to make it work. Think through the bugs that may arise in the given code and consider multiple ways to fix the errors. Doing so will improve your learning as there is always more than one solution and that solution may not be immediately obvious. Incidentally, those are common tasks to assign in an interview: explaining buggy code and then debugging it. Accordingly, these are skills that we hope to help you grow over your course of study at Flatiron!

???

Variables and Scope in Ruby Quiz

Consider the following code:

$best_dog_ever = "Byron the Moyen Poodle"

class OpinionatedClass
  def initialize(dog="Byron the Kleinpudel")
    @dog = dog
  end

  def opinionate!
    puts("#{$best_dog_ever} is the best dog ever")
  end
end

oc = OpinionatedClass.new()
oc.opinionate!

?: What does the $ in front of $best_dog_ever signify?

( ) A protected variable ( ) A static variable ( ) A local variable (X) A global variable

?: What does .new() return (and assign to oc)?

( ) A static copy of the OpinionatedClass class ( ) A reference to the OpinionatedClass constructor (X) An instance of OpinionatedClass ( ) An instance of OpinionatedClass with no instance variables initialized

?: What does oc.opinionate! print?

( ) Trick question, it doesn't emit anything (X) "Byron the Moyen Poodle is the best dog ever" ( ) "undefined is the best dog ever" ( ) " is the best dog ever"

?: Your colleague is asking for some help debugging their code. When they invoke:

oc2 = OpinionatedClass.new()
oc2.opinionate!

they were expecting "Byron the Kleinpudel" to appear somewhere. It doesn't. Without running the code, what do you expect to see? Help your colleague understand what's going on.

(X) In opinionate! the method returns a global versus an instance variable ( ) In opinionate! the method returns a instance versus an global variable ( ) @dog is in the global namespace and needs to be in the class namespace

?: Another colleague is asking for some help tweaking this code. When they invoke:

oc3 = OpinionatedClass.new("Novak the Goldendoodle")
oc3.opinionate!

they want to see Novak the Goldendoodle is the best dog ever. How should they adjust the provided starter code to support this usage?

( ) puts(@dog.to_s + " is the best dog ever") (X) puts("#{@dog} is the best dog ever") ( ) puts(String(@dog) + " is the best dog ever")

?: Will this code work?

oc = OpinionatedClass.new()
oc.dog = "Zebulon T. Bark"

( ) Works (X) Nope, doesn't work

???

ruby-oo-fundamentals-instance-variables-and-scope-quiz's People

Contributors

danielseehausen avatar sgharms avatar bamsarker avatar sbal13 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.