0% found this document useful (0 votes)
4 views2 pages

FCP Lab QP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

FCP Lab QP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

::Q1:: A student is studying the basic concepts of FP.

He wants to understand every


concept to the core. All he knows is that an Armstrong number is a number that is
equal to the sum of cubes of its digits. But he is new to the functional
programming language Scala. Help him in writing a function to print all Armstrong
numbers between given intervals using functions in Scala. Input : Input lower
limit: 1 Input upper limit:1000 Output: Armstrong numbers between 1 to 1000 are: 1,
153, 370, 371, 407.{}

::Q2:: Bill went to a car race and a bike race. Now he is confused about the
difference between the speeds of cars and bikes in races. So that creates a Parent
class called Vehicle with val mph of integer data type and declare race() method
for printing the race type. Now create Child classes Car and Bike that inherit the
Vehicle class override the mph and race() methods. Now, inside the main method,
create a Car object and a Bike object and then access their property mph and method
race.{}

::Q3:: At Hogwarts still, the Triwizard tournament is going on. As Harry won the
first and second challenges still he needs to win the final challenge to reach the
Triwizard cup and final golden egg. For the final task, the champions must reach
the Triwizard Cup and a golden egg located in the hedge maze. The hedge maze is
very tricky and dangerous. Finally, Harry reached the Triwizard Cup and the last
golden egg. To return from the maze, He has to solve the final task in the egg. The
task is- "Implement a class atom and create an object and access the members to
display the element name, electron count, and symbol." .Helpharry in solving the
final challenge to get the Triwizard cup and win the Triwizard tournament.{}

::Q4:: Raj was studying in Harvard University. His teacher ask raj to find the area
of Square and Circle and raj is good in programming and wants to do this by
polymorphism. So mam asked raj to take a base class "Shape" which has a two
functions named "getArea" where one function takes "side" of Int datatype and other
take radius of Double Datatype and write the respective formula for each function
and return the result. We use these functions named "getArea" to find areas. So we
need write a derived class "Area" where this class takes two parameters as side and
radius of Int and Double datatypes respectively. You need to call the function
getArea and give parameters for each one. As the functions return the value you
need to print them in derived class. Note: Create two variables Square and Circle
in derived class to call the getArea methods.{}

::Q5:: Chanduis studying about classes with polymorphic types he need to know
about usage of many functions with the same name. The task is in the first function
we need to print a single integer value and secondtask we need to add 2 integers
and in 3rd task we need product of 3 integers. input format: 120 50,70 10,5,6
output format: First Execution:120 Second Execution:120 Third Execution:300{}

::Q6:: Ram wants to implement a simple tail recursion program to calculate GCD of
given two numbers but he failed to implement the code So you have to implement the
Tail recursion code to find GCD for given two numbers to help him. Hint: Use the
following method definition inyour code @tailrec def gcd(a: Int, b: Int):Int = ……{}

::Q7:: Yosuko was a student in Japan and he was not that much confident incoding.
He came across a problem that is he need to sort the list of numbers using tailrec
concept in scala but Yosuko don't know how to do it and you are the friend of him.
Write the scala code to sort the list of numbers manually using Scala Tail
Recursion concept and help your friend in understanding your code.Input:
[5,8,10,3,1,6] output: [1,3,6,5,8,10]{}

::Q8:: Ravi was a student in Oxford University. He joined after 10 days of college
reopened. So On the first day his mam gave homework on the topic of tail recursion.
The question is you will be give an array ,you need to find the least difference of
factorials of elements in the array. As it was new to him he asked you. Help him to
implement the factorial function using tail recursion concept.Input: [6,2,5,8,3]
Output: 4{}

::Q9:: Karthik is trying to get a webpage's contents when given a url string with
the locationof the webpage. For this purpose, he defined the method as follows :
def getWebpage(url: String): String Using the concept of 'future', changethe
returnvalue of the getWebpage method to some special value that can be returned
immediately.{}

::Q10:: Create a FixedThreadPool to execute our asynchronous code. a. create a


Promise b. create a Runnable and wrap the block to be run asynchronously in the run
method c. close the promise and complete the promise using the result of the run d.
execute the Runnable in the somePool threadpool. e. return the promise.future from
which the caller can read the value.{}

import scala.annotation.tailrec

object FactorialDifference {

@tailrec
def factorial(n: Int, acc: Int = 1): Int = {
if (n <= 1) acc
else factorial(n - 1, n * acc)
}

def leastFactorialDifference(arr: List[Int]): Int = {


val factorials = arr.map(factorial(_, 1))
val diffs = for {
i <- factorials
j <- factorials if i != j
} yield Math.abs(i - j)
diffs.min
}

def main(args: Array[String]): Unit = {


val numbers = List(6, 2, 5, 8, 3)
val result = leastFactorialDifference(numbers)
println(result)
}
}

You might also like