1) What is Scala?
Scala is a functional and object-oriented programming language.
It is used to develop web application, mobile application, enterprise application, desktop based application etc.
Everything in Scala is in object whether it is a number or a function.
2) Write a simple program in Scala to print "Scala Expert" ?
Example:
object TutorialExam
{
def main(args:Array[String])
{
println ("Scala Expert")
}
}
3) What are the features of Scala?
Features of Scala are:
- Traits
- Immutability
- Type inference
- Singleton object
- Lazy computation
- Concurrency control
- Rich collection set
- String interpolation
- Higher order function
- Case classes and Pattern matching
4) Write a program in Scala to print "Scala Expert" by using functional approach ?
Example:
def tutorialExam
{
println("Scala Expert")
}
tutorialExam
5) What are types of data types used in Scala?
Data types are:
Boolean |
Byte |
Short |
String |
Double |
Float |
Long |
Int |
Char |
6) How to create Mutable Variables in Scala?
Example:
var atul = 9
data = 10 // Here atul is variable name
7) How to create Immutable Variables in Scala?
Example:
8) Write a program in Scala to check the number is Even orOdd?
Example:
var number:Int = 40
if(number%2==0)
{
println("Even number")
}
else
{
println("Odd number")
}
9) What will be the output of the following code snippet?
- object Atul123{
- def main(args: Array[String]) {
- var f = 201;
- while( f<=213 ){
- println(f);
- f = f+2
- }
- }
- }
9) What will be the output of the following code snippet?
- object Atul123{
- def main(args: Array[String]) {
- var f = 201;
- while( f<=213 ){
- println(f);
- f = f+2
- }
- }
- }
Output:
201
203
205
207
209
211
213
10) What are the types of comments in Scala ?
There are three types of comments in Scala:
1) Single line.
Example:
2) Multiple line.
Example:
/* Multiple Line Comment.*/
3) Documentation comment.
Example:
/**
* Documentation comment1
* Documentation comment2
*/
11) Is Scala a Pure OOP Language?
Yes, Scala is a Pure Object-Oriented Programming Language because everything in Scala is in object whether it is a number or a function.
It does not have primitive data types and static members.
12) What are the major advantages of Scala Language?
Advantages of Scala language are:
- Immutability
- Interoperability
- More Reusable Code
- Type-Safe Language
- Very Flexible Syntax
- Simple and Concise Code
- Supports all OOP Features
- Highly Scalable and Maintainable code
13) What is Scala set?
It is a collection of elements of the same type. It doesn't contain any duplicate elements.
Scala consists of two types of sets, mutable and immutable.
14) What is Scala Map?
It is a collection of key or value pairs. keys are unique in the map but Values are not unique.
It is also used to store elements.
You can create map by using comma separated pairs or by using rocket operator.
15) What is a Scala Trait?
It is a collection of abstract and non-abstract methods.
It is a special kind of class that enables the use of multiple inheritance.
A trait can extend only one class and a class can have many traits.
Traits cannot be instantiated.
16) Who is the developer of Scala programming language?
Martin Odersky is the developer of Scala programming language.
17) Which command is used to check the version of Scala in Windows?
scala –version command is used to check the version of Scala.
18) Which command is used to check the version of Scala in Linux?
$scala –version command is used to check the version of Scala.
19) What is Closure in Scala?
Closure: It is a function in which the return value depends on the value of one or more variables. The variables are declared outside this function.
20) What are the extensions used in Scala?
.scala and
.sc are the extensions used in Scala.
21) Which command is used to compile the Scala program?
scalac command is used to compile the Scala program.
22) Can we use $ character in Scala identifiers?
No, We cannot use $ character in Scala identifiers because the $ character is a reserved keyword in Scala.
23) What are the types of identifiers available in Scala?
The types of identifiers available in Scala are:
- Alphanumeric Identifiers
- Operator Identifiers
- Mixed Identifiers
- Literal Identifiers
24) What is the syntax to declare a function in Scala?
def functionName ([list of parameters]) : [return type] is the syntax to declare a function in Scala.
25) How to create a String in Scala?
Example:
var greeting = "Tutorial and Example!";
or
var greeting:String = "Tutorial and Example!";
26) What is the syntax for declaring an array variable in Scala?
The syntax for declaring an array variable is:
var z:Array[String] = new Array[String](5)
or
var z = new Array[String](5)
27) What is Pattern matching in Scala?
Pattern Matching: It is widely used feature of Scala after function values and closures. It is used to match the best case available in the pattern. It works same as switch case in other languages.
28) What is unapply method is Scala?
unapply method: It is a method in Scala that is used to match a value and take it apart. It is also used to convert a class into extractor. It reverses the construction process of
apply.