Вы находитесь на странице: 1из 6

Calculation Bureau Java 90 minutes

Question - 1
Buying Show Tickets

There are n people standing in line to buy show tickets. Due to


high demand, the venue sells tickets according to the following
rules:
The person at the head of the line can buy exactly1 ticket
and must then exit the line.
If a person needs to purchase additional tickets, they must
re-enter the end of the line and wait to be sold their next
ticket (assume exit and re-entry takes zero seconds).
Each ticket sale takes exactly 1 second.

We express the initial line of n people as an array, tickets =


[tickets0, tickets1, , ticketsn-1], where each tickets i denotes the
number of tickets that person i wishes to buy. If Jesse is located
at position p, how many seconds will it take for him to purchase
tickets p tickets?

For example, if tickets = [1, 2, 5] and p = 1, the first five seconds


of ticket sales look like this:

Green denotes Jesse, and gray denotes a person that


left the line.
Jesse finishes purchasing all tickets 1 = 2 tickets at time
t = 4.

Complete the waitingTime function in the editor below. It has two


parameters:
1. An array, tickets , of n positive integers describing the initial
sequence of people standing in line. Each tickets i describes
the number of tickets that the person waiting at initial
position i needs to purchase.
2. An integer, p, denoting Jesse's position in tickets .
The function must return an integer denoting the number of
seconds it takes for Jesse to finish purchasing exactly tickets p
tickets.

Input Format
Locked stub code in the editor reads the following input from
stdin and passes it to the function:
The first line contains an integer, n, denoting the number of
elements in tickets .
Each line i of the n subsequent lines contains an integer
describing tickets i.
The last line contains an integer, p.

Constraints
5
1 n 105
1 tickets[i] 109, where 0 i < n.
0p<n

Output Format
Return an integer denoting the number of seconds it takes for
Jesse to finish purchasing exactly tickets p tickets.

Sample Input 0

5
2
6
3
4
5
2

Sample Output 0

12

Explanation 0
Given tickets = [2, 6, 3, 4, 5], Jesse's wait time looks like this:
0. window 2 6 3 4 5
1. window 6 3 4 5 1
2. window 3 4 5 1 5
3. window 4 5 1 5 2
4. window 5 1 5 2 3
5. window 1 5 2 3 4
6. window 5 2 3 4 (the person at the head of the line
in the previous step purchased their last ticket and does not
re-enter the line)
7. window 2 3 4 4
8. window 3 4 4 1
9. window 4 4 1 2
10. window 4 1 2 3
11. window 1 2 3 3
12. window 2 3 3 (Jesse purchased his last ticket and
does not re-enter the line)
Because it took a total of twelve seconds for Jesse to purchase
all tickets 2 = 3 tickets, we return 12.

Sample Input 1

4
1
1
1
1
0

Sample Output 1

Explanation 1
Given tickets = [1, 1, 1, 1], Jesse's wait time looks like this:
0. window 1 1 1 1
1. window 1 1 1 (Jesse purchased his ticket and does not
re-enter the line)
Because it took a total of one second for Jesse to purchase
tickets 0 = 1 ticket, we return 1.

Sample Input 2

4
5
5
2
3
3

Sample Output 2

11

Given tickets = [5, 5, 2, 3], Jesse's wait time looks like this:
0. window 5 5 2 3
1. window 5 2 3 4
2. window 2 3 4 4
3. window 3 4 4 1
4. window 4 4 1 2
5. window 4 1 2 3
6. window 1 2 3 3
7. window 2 3 3 (the person at the head of the line in
the previous step purchased their last ticket and does not
re-enter the line)
8. window 3 3 1
9. window 3 1 2
10. window 1 2 2
11. window 2 2 (Jesse purchased his last ticket and does not
re-enter the line)
Because it took a total of eleven second for Jesse to purchase all
tickets 3 = 3 tickets, we return 11.

Question - 2
Animal Inheritance

The locked code in the editor does the following:


1. Declares an abstract class named Animal with the
implementations for getIsMammal() and getIsCarnivorous()
methods, as well as an abstract method named
getGreeting() .
2. Creates Dog, Cow, and Duck objects.
3. Calls the getIsMammal() , getIsCarnivorous() , and
getGreeting() methods on each of these respective objects.

Consider the following UML diagram:

A UML diagram of Animal, Dog, Cow, and Duck classes.


Recall that - denotes private, + denotes public, and #
denotes protected.

Complete the code in the editor below by writing the following:


1. Three classes named Dog , Cow , and Duck that inherit
from the Animal class.
2. No-argument constructors for each class that initialize the
instance variables inherited from the superclass.
3. Each class must implement the getGreeting() method:
For a Dog object, this must return the string ruff .
For a Cow object, this must return the string moo .
For a Duck object, this must return the string quack .

Input Format
There is no input for this challenge.

Output Format
The getGreeting() method must always return a string denoting
the appropriate greeting for the implementing class.

Sample Output

A dog says 'ruff', is carnivorous, and is a mammal.


A cow says 'moo', is not carnivorous, and is a mammal.
A duck says 'quack', is not carnivorous, and is not a
mammal.

Question - 3

What is the expected result?

import java.util.*;

public class MyPancake implements Pancake {

public static void main(String[] args) {


List<String> x = new ArrayList<String>();
x.add("3"); x.add("7"); x.add("5");
List<String> y = new MyPancake().doStuff(x);
y.add("1");
System.out.println(x);
}

List<String> doStuff(List<String> z) {
z.add("9");
return z;
}
}

interface Pancake {
List<String> doStuff(List<String> s);
}

[3, 7,
5]

[3, 7, 5,
9]

[3, 7, 5, 9,
1]

Compilation fails.

An exception is thrown at
runtime.

Question - 4

What is the result of compiling and running the following


program?
class test {
public static void main(String args[]) {
int[] arr = {1,2,3,4};
call_array(arr[0], arr);
System.out.println(arr[0] + "," + arr[1]);
}
static void call_array(int i, int arr[]) {
arr[i] = 6;
i = 5;
}
}

1,2

5,2

1,6

5,6

Question - 5

What is the output for the below code ?

1. public class A {
2. int add(int i, int j){
3. return i+j;
4. }
5. }
6. public class B extends A{
7. public static void main(String argv[]){
8. short s = 9;
9. System.out.println(add(s,6));
10. }
11.}

Compile fail due to error on line no


2

Compile fail due to error on line no 9 due to static method


referenced from a static context.

Compile fail due to error on line no 9 due to type


mismatch.

15

Question - 6

Which of the following statements are correct. Select the one


correct answer.

Each Java file must have exactly one package statement to


specify where the class is stored.
If a Java file has both import and package statement, the import
statement must come before package statement.

A Java file has at least one class


defined.

If a Java file has a package statement, it must be the first


statement (except comments).

Question - 7

What is the output for the below Java code ?

public class Test {


public static void main(String[] args){
int i = 010;
int j = 07;
System.out.println(i);
System.out.println(j);
}
}

87

10 7

C.Compilation fails with an error at line


3

D.Compilation fails with an error at line


5

Вам также может понравиться