Question

In: Computer Science

What is the output from the following program? Explain your results. file:///Users/brianabernal/Downloads/Question%201.pdf int val = 20;...

What is the output from the following program? Explain your results.

file:///Users/brianabernal/Downloads/Question%201.pdf

int val = 20;

int func1()
{
    int val = 5;
    return val;
}

int func2()
{
    return val;
}

int main()
{
    // (1)
    cout << func1() << endl;

    // (2)
    cout << func2() << endl;
}

Solutions

Expert Solution

Whatever is needed to answer this question is already in the code given so I'm assuming the pdf file directory that you hvae provided is not relevant here.

The output of the code is as given below:

The function func1() gives output as 5 and func(2) as 20 because of a simple concept that c++ uses called "scope of a variable". Here we see that int val = 20; in this a variable named val is declared globally i.e. its existence is for the entire code and wherever it is used, its value will be retrieved from the stack record created and that's why when func2() calls it for printing, it is recognized by the compiler and not the variable val declared in func1() because that val is separately created in the stack memory and its scope is only within func1().


Related Solutions

The chart is here. file:///C:/Users/tyler/Downloads/Standard%20Normal%20Table%20(1).pdf 1.      Find the area under the normal curve that is: greater than...
The chart is here. file:///C:/Users/tyler/Downloads/Standard%20Normal%20Table%20(1).pdf 1.      Find the area under the normal curve that is: greater than Z = 0.64 2.      Find the area under the normal curve that is: below Z = -1.53 3.      Find the area under the normal curve that is: greater than Z = -1.89 4.      Find the area under the normal curve that is: below Z = 2.27 5.      Find the area under the normal curve that lies between: Z = 0.00 and Z = 0.85 6.      Find the area under...
In the PDF file that will contain your screen shots, explain what is wrong with the...
In the PDF file that will contain your screen shots, explain what is wrong with the code below. Do not just state thing like “line 3 should come after line 7,” you must explain why it needs to be changed. This shows that you are fully understanding what we are going over. // This program uses a switch-case statement to assign a // letter grade (A, B, C, D, or F) to a numeric test score. #include <iostream> using namespace...
Question 9 What is produced from the following code segment? val = 0; do { val++;...
Question 9 What is produced from the following code segment? val = 0; do { val++; WriteLine(val); } while (val < 5); Nothing is displayed Outputs 5 thru 9 Outputs 10 Outputs 1 thru 4 Outputs 1 thru 5 Outputs 10 thru 14 Outputs 0 thru 4 Question 10 Assuming val was described as an integer and an initial value of 10, what is produced from the following code segment? do { WriteLine(val); } while (val < 10); Nothing is...
What is the expected output from the following program (3 answers) ______­­ ______namespace std; double insurance(int);...
What is the expected output from the following program (3 answers) ______­­ ______namespace std; double insurance(int); void main() { int j; ______ mileage; ______ monthly_rent; for (j=______ j<4; j++) { mileage=1000*j; monthly_rent= 0.3*mileage + insurance(mileage); printf("Monthly rent for %4d.2f is : $ ______ . \n", mileage, monthly_rent); } } double insurance(int miles) { double mileage_charge; if (miles<=1000) { mileage_charge=100.0; }___ if ((miles>1000) && (miles<=2000)) { mileage_charge=150.0; }; ___ (miles>2000) { mileage_charge=200.0; }; return(mileage_charge);
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
What output is produced by the following code fragment? int num = 0, max = 20;...
What output is produced by the following code fragment? int num = 0, max = 20; while (num < max) { System.out.println(num); num += 4; }
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha = 'A'; for(int i = 0; i < 13; i++){ for(int j = 0; j < 2; j++){ cout << alpha; alpha++; } } cout << endl; return 0; }
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h>...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 3 /* create thread argument struct for thr_func() */ typedef struct _thread_data_t {   int tid;   double stuff; } thread_data_t; /* thread function */ void *thr_func(void *arg) {   thread_data_t *data = (thread_data_t *)arg;   printf("hello from thr_func, thread id: %d\n", data->tid);   pthread_exit(NULL); } int main(int argc, char **argv) {   pthread_t thr[NUM_THREADS];   int i, rc;   thread_data_t thr_data[NUM_THREADS];   /* create threads */   for (i = 0;...
Exercise: What does the following program guess.py do? Write your answer in the file explanation.txt. guess.py...
Exercise: What does the following program guess.py do? Write your answer in the file explanation.txt. guess.py program import random number = random.randint(1, 25) number_of_guesses = 0 while number_of_guesses < 5: print('Guess a number between 1 and 25:') guess = input() guess = int(guess) number_of_guesses += 1 if guess == number: break
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT