Consider the following definition of a recursive method. public static int mystery(int[] list, int first, int last) { if (first == last) return list[first]; else return list[first] + mystery(list, first + 1, last); } Given the declaration int[] alpha = {1, 4, 5, 8, 9}; What is the output of the following statement? System.out.println(mystery(alpha, 0, 4)); a. 1 b. 18 c. 27 d. 32

Answers

Answer 1
Answer:

Answer:

c. 27

Explanation:

  • In recursion method, a method calls itself until a condition becomes true and returned back. In mystery(), at first time, first is 0 and last is 4. Condition is checked and 0!=4 so else part is executed in which again mystery is called with first as 1 and last 4. This will go again and again until first=4.
  • When first=4 it is also equal to last so if part is executed and alpha[4] is returned which is 9, now it comes alpha[3] +9 which is 8+9 =17 .
  • It is returning values to its previous calls that is why it will reduce to alpha[0]+alpha[1]+alpha[2]+17 which is nothing but sum of all elements of a alpha
  • Then, 1+4+5+8+9=27.

Related Questions

Failing to include a complete ____ on each side of an && operator in an if statement is a common error in Java programming.a. operatorb. mathematical expressionc. variabled. Boolean expression
Devices may be arranged in a _____ topology. A) mesh B) ring C) bus D) all of the above
Given six memory partitions of 100 MB, 170 MB, 40 MB, 205 MB, 300 MB, and 185 MB (in order), how would the first-fit, best-fit, and worst-fit algorithms place processes of size 200 MB, 15 MB, 185 MB, 75 MB, 175 MB, and 80 MB (in order)? Indicate which—if any—requests cannot be satisfied. Comment on how efficiently each of the algorithms manages memory.
3. Problem 5. A digital computer has a memory unit with 24 bits per word. The instruction set consists of 150 different operations. All instructions have an operation code part (opcode) and an address part (allowing for only one address). Each instruction is stored in one word of memory. a. How many bits are needed for the opcode? b. How many bits are left for the address part of the instruction? c. What is the maximum allowable size for memory? d. What is the largest unsigned binary number that can be accommodated in one word of memory?
Write an application that calculates and displays the amount of money a user would have if his/her money could be invested at 5% interest for one year. Create a method that prompts the user for the starting value of the investment and returns it to the calling program. Call a separate method to do the calculation and return the result to be displayed. Save the program as Interest.java

Write a program that asks the user to enter the size of a triangle (an integer from 1 to 50). Display the triangle by writing lines of asterisks. The first line will have one asterisk, the next two, and so on, with each line having one more asterisk than the previous line, up to the number entered by the user. On the next line write one fewer asterisk and continue by decreasing the number of asterisks by 1 for each successive line until only one asterisk is displayed. (Hint: Use nested for loops; the outside loop controls the number of lines to write, and the inside loop controls the number of asterisks to display on a line.) For example, if the user enters 3, the output would be:_______.a. *b. **c. ***d. **e. *

Answers

Answer:

Implemented using Python

n = int(input("Sides: "))

if(n>=1 and n <=50):

    for i in range(1,n+1):

         for j in range(1,i+1):

              print('*',end='')

         print("")

       

    for i in range(n,0,-1):

         for j in range(i,1,-1):

              print('*',end='')

         print("")

else:

         print("Range must be within 1 and 50")

Explanation:

This line prompts user for number of sides

n = int(input("Sides: "))

The line validates user input for 1 to 50

if(n>=1 and n <=50):

The following iteration uses nested loop to print * in ascending order

   for i in range(1,n+1):

         for j in range(1,i+1):

              print('*',end='')

         print("")

The following iteration uses nested loop to print * in descending order        

    for i in range(n,0,-1):

         for j in range(i,1,-1):

              print('*',end='')

         print("")

The following is executed if user input is outside 1 and 50

else:

         print("Range must be within 1 and 50")

Write a simple arithmetic expression translator that reads in expressions such as 25.5 + 34.2 and displays their value. Each expression has two numbers separated by an arithmetic operator. (Hint: Use a switch statement with the operator symbol (type char) as a selector to determine which arithmetic operation to perform on the two numbers. For a sentinel, enter an expression with zero for both operands.)

Answers

Answer:

Following are the program in the C++ Programming Language.

//header file

#include<iostream>

//header file

#include <bits/stdc++.h>

//namespace

using namespace std;

//set main function

int main()

{

//set float type variables

float a,s,m,d,c,b,g;

//print message and get variable from the user

cout<<" Enter The First Number : ";

cin>>c;

//print message and get variable from the user

cout<<" Enter The Second Number: ";

cin>>b;

again:

//get variable from the user for Operation

cout<<" Enter the Operation which you want : ";

char choice;

cin>>choice;

//Addition

a=c+b;

//Subtraction

s=c-b;

//Multiplication

m=c*b;

//Division

d=c/b;

//Modulus

g=fmod(c, b);

//set switch statement

//here is the solution

 switch(choice)

 {

   case '+':  

     cout<<"Addition: "<<a<<endl;

     goto again;

     break;

   case '-':

     cout<<"Subtraction: "<<s<<endl;

     goto again;

     break;

   case '*':

     cout<<"Multiplication: "<<m<<endl;

     goto again;

     break;

   case '/':

     cout<<"Division: "<<d<<endl;

     goto again;

     break;

   case '%':

     cout<<"Modulus: "<<g<<endl;

     goto again;

     break;

   default:

     cout<<"Exit";

     break;

 }

return 0;

}Explanation:

Here, we define the required header files then, we set main function.

  • set float type variables a,s,m,d,c,b,g.
  • Get input from the user for operation in the variable c,d.
  • Set character type variables in which we get input from the user for operations.
  • Then, we perform some operations.
  • Set switch statement which display the output according to the user an if user input wrong then statement breaks.

____ produces a full-featured, working model of the information system. A. Loose coupling B. Fourth-generation modeling C. System prototyping D. Web servicing

Answers

Answer:

C.system prototyping

Explanation:

Prototyping produces a quickly constructed working version of the proposed information system.

Planning

Analysis

Design

System prototyping

Implementation

Answer:

c. system prototyping

Explanation:

system prototyping  produces a full-featured, working model of the information system.

Whichof following can be thrown using the throw statement?ErrorThrowableExceptionRuntimeExceptionAll of Given

Answers

Answer: Exception

Explanation: A throw statement is a statement that whenever it gets executed ,it stops the flow of the execution immediately. The throw statement has a throw keyword for stopping of the execution which is denoted as the exception. A checked or unchecked exception can only be thrown. The compiler of a program has the function of giving warning if there are chances of exception.

A computer professional who has access to sensitive information shares this information with a third party. Which professional code of conduct did the person violate? A. diligence in service B. company discipline norms C. confidentiality of information D. conflict of interest

Answers

Answer:

Confidentiality of Information

Explanation:

Because when you work for a company that confidential information, the company usually asks it employees not to discuss or share information with anyone. The company usually asks their employees to sign a Confidentiality Contract.

Which of the following statements is not true?A. A structured chart is a sequential representation of program designB. the Real-Time system is a particular case of a on-line-systemC. Batch totals are not incorporated while designing real-time applicationsD. 4GLs are used for application proto typingE. None of the above

Answers

Answer:

A structured chart is a sequential representation of program design

Explanation:

It is true that a real-time system which is a term to describe an operating system working in relation to real-time is actually a form of the online system. Hence, option B is not correct.

It is also true that Batch totals are not incorporated while designing real-time applications because Batch Data processing is carried out in a separate manner and at a time when the computer is free. Thus Option C is not Correct

It is also true that 4GL which stands for Forth generation programming language is used for application prototyping. Again Option D is not Correct.

However, a structured chart is not a sequential representation of program design, but rather a break down to the infinitesimal module in the program design. Hence, option A is the correct answer.