Scala Flashcards

1
Q

What is the JDK and what does it let us do?

A

JDK stands for Java Development Kit. It is a package for developing Java based software. It will take our source code and convert them to a format that the JRE or JVM can execute. It includes things such as the Debugger for finding errors and the Java Compiler for compiling our code. Java developers will use the JDK as an environment to develop, compile, and run java applications.

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

What is the JRE and what does it let us do?

A

JRE stands for Java Runtime Environment. The JRE is used to RUN your java program. The JRE contains classes and the JVM which will be used to run your Java Program.

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

What is JVM and what does it let us do?

A

JVM stands for Java Virtual Machine. It provides the Java Runtime Environment for which java bytecode (.class java file) can be executed. The tasks of the JVM include loading, verifying, and executing the code and this is done line by line.

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

What is Git?

A

Git is a version control system that lets you track and make changes to your files. It keeps a record of what you have done and lets you revert to specific versions of your files. Git allows for easier collaboration between multiple people because it allows you to keep all your files merged into one source. Git allows you to save and develop on parallel versions of your code base.

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

What is Github?

A

Github is a hosting service that lets you manage your git repositories. It provides a graphical interface to let you see and manage your projects.

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

How does scala relate to Java?

A

Java and Scala are both Object Oriented programming languages. Scala like Java also utilizes the JVM meaning scala code will be converted to the same bytecode as java, thus they share the same memory space, types and JVM.

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

Is scala statically typed or dynamically typed?

A

Scala is a statically typed language. Statically typed languages mean that types are checked at compile time as opposed to run time. So for example if you have an Int value it will ensure that the value is used correctly throughout the program and at runtime nothing other than an int can be in that values memory location.

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

Do we need to declare type for Scala?

A

Scala has a type inference system meaning that if you declare a variable to be of value 1 the compiler will know that the variable is type int. With this system, it is not necessary to declare types although there will be times where you might need to declare types explicitly for better code readability.

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

What are integer types in Scala?

A

Integer types are represented in scala by int, long and short. Int is a 32 bit two’s complement integer where as short is a signed 16 bit two’s complement integer and long is a 64 bit two’s complement integer.

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

What are decimal types in Scala?

A

Decimal types are represented by floats and doubles with float being 32 bits and double being 64 bits.

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

What is the difference between val and var?

A

The difference between val and var is that val makes a variable immutable and var makes variable mutable. Val is similar to that of final in java where when declared it cannot change while a var variable can change throughout a program’s lifetime.

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

What is a pure function?/What is a side effect?

A

A pure function doesn’t depend on and doesn’t modify the states of variables out of its scope. If you call pure functions with the same set of arguments you will always get the same return values. They do not modify the arguments which are passed to them.

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

Why do we make use of programming paradigms like Functional Programming and Object Oriented Programming?

A

Functional programming helps us solve problems in a simpler way. It improves modularity and maintainability. It breaks down large projects into simpler modules so that we can test these modules individually. Also by having all of our functions as pure functions we don’t have to worry have side effects that can happen so we can debug the code more quickly. Same thing with OOP it helps with modularity. Because everything is

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

What is the significance of the Any type in Scala?

A

Any in Scala is the supertype of all types. Every class in scala inherits either directly or indirectly from this class.

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

What does it mean that functions are first class citizens in Scala?

A

First-class functions are functions that can be treated like any other value. You can pass them to functions as arguments, return them from functions, and save them in variables.

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

What is a REPL?

A

The scala REPL stands for Read, Evaluate, Print, and Loop. It is just a tool on the command line that lets you write interactive scala code.

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

What’s the difference between an expression and a statement?

A

An expression usually refers to a piece of code that can be evaluated to a value, and is composed of variables, operators, function calls, and literals. A statement refers to a piece of code that executes a specific instruction or tells the computer to complete a task.

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

What is a higher ordered function?

A

Higher order functions take other functions as parameters or return functions as a result. An example of this could be the filter function that takes a predicate as a parameter, with predicate being just a function or method that returns a boolean value.

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

Why might we want to write an application using pure functions instead of impure functions?

A

One of the main advantages of writing pure functions vs impure functions are that they are easier to test and debug. Output only solely depends on input and nothing outside the scope of the function. There are no side effects or hidden input outputs to worry about.

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

How do operators work in scala?What is the + operator?

A

In Scala, operators are methods. Any method with a single parameter can be used as an infix operator. For example, + can be called with dot-notation

21
Q

How do we define an auxiliary constructor?

A

Auxiliary constructors are used for constructor overloading in Scala and are defined as a method using THIS name. They must call previously defined constructors or the primary constructor in the first line of its body.

22
Q

List

A

A list is a collection that contains immutable data meaning once its created it cannot be changed. Each element in a list does not have to be the same data type. In scala the list represents a linked list and are zero indexed.

23
Q

Set (methods)

A

A set is a collection that only contains unique elements meaning their are no duplicate elements. Sets can be immutable and mutable and by default are immutable.

24
Q

Map(collection)

A

A scala map is a collection of key/value pairs. Values can retrieved based off their key. Keys are unique in maps while values are not. Maps can be immutable and mutable and by default are immutable.

25
Q

ArrayBuffer

A

An arraybuffer is just a collection that stores elements of the same data type but unlike an array its size is not fixed. You can add and delete from the array. Arraysbuffers are also mutable and zero indexed.

26
Q

Array

A

An array is just a collection in scala that stores elements of the same data type and is fixed in size. An array is mutable and is also zero indexed.

27
Q

What are exceptions?

A

Exceptions are events that change the normal flow of a program. Usually when something bad and unexpected has occurred. We as programmers should write code to throw and catch exceptions when they occur. We can handle this by writing try,catch and finally blocks. The try block is where we put the risky code. The catch block will have the case statements to match different exceptions and the finally block will have code that will always be executed.

28
Q

What are errors?

A

Errors occur when there are problems with the jvm itself, usually with the machinery that runs our applications. Common errors can be outofmemory error which occurs when you use too much memory storing objects or the stackoverflow error when you call too many functions or methods.

29
Q

What is throwable?

A

Throwable is a class that is the superclass of all errors and exceptions.

30
Q

Tuple

A

A tuple is a collection that contains a fixed number of elements each with its own type. Tuples are immutable and start at index 1 but can only store and maximum of 22 elements.

31
Q

Concurrency

A

Concurrency is when more than one task can start and complete in overlapping time periods. It doesn’t matter whether they’re running at the same instant. You can write concurrent programs on a single CPU machine where only one task can execute at a given point of time. Future and promise can be used for concurrency. A future represents a value which may or may not be currently be available but will be available at some point. A promise is a wrute handle to a value that will be available at some point in the future.

32
Q

Trait

A

Traits are similar to java interfaces in that they are reusable components that can be used to extend the behavior of classes. They can contain both abstract and concrete methods and properties. Classes and objects can extend traits but traits cannot be instantiated and therefore have no parameters.

33
Q

Curry

A

Currying is the process of converting a function with multiple arguments into a sequence of functions that take one argument.

34
Q

Singleton Object

A

Singleton Object is an object which has one instance which is used in the entire program. In scala, there is no static concept. So scala creates a singleton object to provide entry point for your program execution. Methods declared inside Singleton Object are accessible globally. A singleton object can extend classes and traits.

35
Q

flatmap

A

Flatmap is just an almost identical version of the map method where the mapping is applied to the collection but it takes it one step further and generatesa sequence.

36
Q

Map

A

The map function just takes one collection and transforms it to another collection by applying a function on every single element. A basic example could be you have a list A of integers and want ot

37
Q

Define Scala?

A

Scala is high level programming language that supports both functional and object oriented programming. Scala is built on top of Java and and is statically typed meaning variables cannot change type. Scala also has a type inference system meaning variable types do not explicitly need to be declared. Like java, scala code is compiled down to bytecode and run on the jvm.

38
Q

How is Scala a programming language with both features of functional and object-oriented programming?

A

Scala is a pure object-oriented language in the sense that every value is an object. It’s even more object-oriented than Java, by that I mean that any value, even integers and doubles are objects! this opens a new world of no-special treatment for primitive values.

39
Q

What is recursion in Scala?

A

Recursion in Scala just means a function can call itself repeatedly.

40
Q

How do you get distinct values from a list?

A

You can use the distinct method.

41
Q

How do you drop “n” elements from a list?

A

You can use the .take method which returns the first n elements from a list.

42
Q

How do you reverse the contents of a list?

A

You can use the .reverse method but you need to reassign this to a new list. For example val newList = list.reverse

43
Q

Explain the syntax for function declaration in Scala?

A

first is the keyword def used to declare functions and methods, followed by the function name, followed by any list of parameters, followed by the return type and then the body.

44
Q

What are the different scopes for variables in Scala?

A

The three scopes are fields, method parameters, and local variables. Fields can be accessed from every method in the object and outside the object if we declare them with the right access modifiers. Method parameters are used to pass values into a method and they can be accessed inside or outside if their is a reference to the object from the outside world. Local variables are declared inside a method and can only be accessed inside of the method only.

45
Q

What is extend keyword?

A

The extend keyword is used in scala to extend a class meaning that a class is inherited from another class.

46
Q

What are the different types of access modifiers in Scala?

A

The different access modifiers are public, private, and protected. Private members are only accessible inside the class or to the object containing the member definition. Protected members can be accessed from subclasses of the class in which the member is defined. Public modifiers are the default access modifiers and can be accessed anywhere.

47
Q

What is pattern matching in Scala?

A

Pattern matching is a way of checking the given sequence of tokens for the presence of the specific pattern. It is the most widely used feature in Scala. It is a technique for checking a value against a pattern. Some common ways of pattern matching are by using the match expression or defining regex patterns.

48
Q

What are option, some, and none in Scala?

A

use the Option class when returning a value from a function that can be null. Simply stated, instead of returning one object when a function succeeds and null when it fails, your function should instead return an instance of an Option, where the instance is either:

    An instance of the Scala Some class
    An instance of the Scala None class

Because Some and None are both children of Option, your function signature just declares that you’re returning an Option that contains some type

49
Q

Explain variable arguments?

A

Varargs allows us to indicate that the last argument of the function is a variable length argument. it may be repeated multiple times. It allows us to indicate that last argument of a function is a variable length argument, so it may be repeated multiple times. we can pass as many argument as we want. Helps us avoid writing boilerplate code by introducing syntax that can handle an x amount of paramters automatically.