April 2015 Flashcards

1
Q

PEMDAS. What is the sentence behind this acronym?

A

Please Excuse My Dear Aunt Sally

Parentheses, Exponents, Multiplication, Division, Addition, Subtraction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

RUBY
What is the meaning of the unless keyword in ruby?
unless age >= 18

A

if age < 18

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Ruby

how to write an infinite loop?

A

loop do
expression
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Ruby

Write a for loop which executes an expression 5 times

A

5.times do
expression
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Ruby

Is it allowed to have arrays that contain object of different types?

A

yes!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Ruby

How can I add the string “foo” to the array [1,2,3]

A

[1,2,3].push(“foo”) or

[1,2,3] &laquo_space;“foo”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Ruby

Multiply every element in the array [1,2,3] by 3 using the map

A

[1,2,3].map { |i| i*3}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Ruby

Filter the even numbers from the array [1,2,3,4] using select

A

[1,2,3,4].select { |number| number % 2 == 0 }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Ruby

How can I loop over the array [1,2,3] with each

A

[1,2,3].each do |number|
new_array &laquo_space;number
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Ruby

Create a hash map with some strings as keys and some integers as values

A

my_map = {“key1” => 1, “key2” => 2}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Ruby

Given the map my_map = {“key1” => 1, “key2” => 2}, retrieve the value of the “key1”

A

my_map[“key1”]

returns 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
Ruby
How to get the class of an object? For example get the class of object 1
A
1.class
returns FIXUM (a special kind of integer)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Ruby

check if 1 is of type Integer

A

1.is_a?(Integer)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Ruby

Write a method that adds 2 to a number

A

def add_two(number)
number.next.next
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Ruby

How to define a function with unknown number of parameters?

A

Using the splat operator which is a *
def my_function(*argument)
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Ruby

add an option to the add(num1,num2) that returns the absolute value if the option is true

A
def add(num1, num2, options = {})
   sum = num1 + num2
   sum = sum.abs if options[:absolute]
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Ruby

Define a single line lambda that returns a string

A

l = lambda {“foo bar”}

puts l.call

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Ruby

Define a multiline lambda with an argument. If argument is “try” print something and else, print something else

A
l = lamda do |input|
if string== "try"
   return "foo"
else
   return "bar"
end
puts l.call("try")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Ruby

What is the difference between modules and classes?

A

Modules have only behaviour but classes have behaviour and state

20
Q
Ruby
Define a module and include it into a class
A
module my_module
  def my_module_method
  end
end
class Gym
  include my_module
end
21
Q

Ruby

How to open a file named a.txt in read-write mode

A
mode = "r+"
file = File.open("a.txt", mode)
22
Q

Ruby

How does the inject method work in ruby?

A

[1, 2, 3, 4].inject(0) { |result, element| result + element }

It takes an initial value which is zero in the example. It starts with result = 0 and element equal to the first element in the array. In the second iteration it has again two arguments. The first one is the result of the first iteration and the second one is the second element of the array

23
Q
Ruby
Define a class A that inherits from the class B
A
class A < B
end
24
Q
Ruby
Define a class A with a setter method set to set the variable var
A
class A
  def var=(var)
    @var = var
  end
end
25
Q
Ruby
What is an attr_writer method? Define a class an an attribute write method for setting the properties color and description
A

class A
attr_write :color, :description
end

26
Q
Ruby
What is an attribute reader method? Define a class with an attribute reader to set the variables color and description
A

class A
attr_reader :color, :description
end

27
Q

Ruby

How to start the interactive ruby shell?

A

type irb in the console

28
Q

Ruby

How can I define a static method inside a class?

A

class A
def self.method
end
end

29
Q

Ruby

How to overwrite the equals method in a class?

A

overwrite the method
def ==(other_item)
end

30
Q

Ruby

Assume that we implement the == method but unique still does not return unique elements? Why?

A

== is not fast. unique uses the hash codes to compare the object. This method has to be implemented.

31
Q

Ruby

What is the difference between p and puts?

A

puts returns results of to_s and p returns the results of the inspect method

32
Q

Ruby

What is the meaning of serialization?

A

Turning objects into strings. Making them serial!

33
Q

Ruby

define a method serialize to serialize an object into YAML

A

def serialize
YAML::dump(self)
end

34
Q

Ruby

define a method deserialize to deserialize from YAML

A

def deserialize(yaml_string)
YAML::load(yaml_string)
end

35
Q

Ruby

How to remove all nil elements from an array?

A

[1,2,nil,3,nil,5].compact

36
Q

Ruby

convert array [1,2,3] to a string where elements are separated by comma

A

[1,2,3].join(“, “)

37
Q

What is the fundamental theorem of arithmetic?

A

Every natural number greater than one can be expressed as a product of prime numbers

38
Q

What is XPath?

A

It is a syntax for defining parts of an xml document and also navigating in xml documents

39
Q

What are atomic values in XPath?

A

values with no children or parent

40
Q

xpath

How to select all nodes with the name “nodename”

A

nodename

41
Q

xpath

how to select all book elements that are children of bookstore?

A

bookstore/books

42
Q

xpath

how to select the first book element under bookstore?

A

bookstore/book[1]

43
Q

xpath

how to select the last book element under bookstore?

A

bookstore/book[last()]

44
Q

xpath

how to select the first two books under bookstore?

A

bookstore/book[position() < 3]

45
Q

xpath

select all title element that have an attribute lang

A

title[@lang]

46
Q

جنگ ويتنام از چه سالي تا چه سالي بود؟

A

١٩٦٠ تا ١٩٧٥