Question

1. What would be the output of the following lines of code: int num = 2;...

1.

What would be the output of the following lines of code:

int num = 2;

switch(num){
    case 1: 
        cout << "1";
    case 2:
        cout << "2";
    case 3:
        cout << "3";
    case 4:
        cout << "4";
}

2.

What would be the output of the following code:

char op = '*';

switch(op){
    case '+': 
        cout << "Addition";
        break;
    case '-':
        cout << "Subtraction";
        break;
    case '/':
        cout << "Division";
        break;
    default:
        cout << "Invalid";
}

3.

What would be the output of the following code:

int num = 2;

switch(num){
    case 1: 
        cout << "1";
        break;
    case 2:
        cout << "2";
    case 3:
        cout << "3";
        break;
    case 4:
        cout << "4";
        break;
}

4.

Give that the file "input.txt" contains the following text:

Quintin Donnelly123

After the following code is executed

ifstream fin;
string firstName = ' ';
string lastName = ' ';
int num = 0;

fin.open("input.txt");
fin >> firstName >> lastName >> num;
fin.close();

What are the values contained in

firstName:

lastName:

num:

5.

Given the file "input.txt" contains the following text:

100 + 20

What would be the output of the following code:

ifstream fin;
int num1, num2;
char op;

fin.open("input.txt");
fin >> num1 >> op >> num2;
fin.close();

switch(op){
    case '+':
        cout << num1+num2;
        break;
    case '-':
        cout << num1-num2;
        break;
    case '*':
        cout << num1*num2;
        break;
    case '/':
        cout << num1/num2;
        break;
    default:
        cout << "Invalid equation.";
}

6.

Given the file "input.txt" contains the following text:

outputMe

Fill in the blank to write to an output file:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() {
    string data;
    ifstream input("input.txt");
    ofstream output("output.txt");
    // get the input from the file
    input >> data;
    // close the input file
    input.close();
    // write to output file 
    [BLANK] << data;
    // close the ouptutfile
    output.close();
    return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q.1.

Answer : 234

Explanation: Since the value of num=2 so case 2 will match and since break is not used so it will execute all the case blocks starting from 2.

Q2.

Answer : Invalid

Explanatio Since value of op='*' but in switch there is no case block for '*' so it will execute the default block.

Q3.

Answer:23

Explanation:  Since the value of num=2 so case 2 will match and since break is not used so it will execute all the case blocks starting from 2 but in case 3 since break is used so it will terminate the switch block after case -3 will execute.

Q4.

Answer :

Quintin Donnelly123

Explanation : The data from file will be read and store these in firstName,lastName and num so the output will be Quintin Donnelly123

Q5.

Answer :120

Explanation: The data will be read from input.txt and store them in num1,num2and op. So num1 will store 100, op will store + and num2 will store 20

Now op will pass to switch case and case'+' will match so it will execute the block and the result will be 120.

Q6.

Answer :It will show error.
Explanation: input >> data; this statement will read the data from input.txt and store it in data. so the value of data will be outputMe. But the next statement

[BLANK] << data; is invalid because of blank. So it will show error. But if you will replace the blank by output object as output<<data, then it will copy the value of data into output.txt.
Add a comment
Know the answer?
Add Answer to:
1. What would be the output of the following lines of code: int num = 2;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include #include #include using namespace std; int main() { // Declare variables here ifstream fin; string...

    #include #include #include using namespace std; int main() { // Declare variables here ifstream fin; string flowerName; string flowerGrow; // Open input file fin.open("flowers.dat"); fin >> flowerName; fin >> flowerGrow; while (!fin.eof()) { cout << flowerName << "grows in the" << flowerGrow << endl; // Write while loop that reads records from file. fin >> flowerName; fin >> flowerGrow; // Print flower } cout << flowerName << "grows in the" << flowerGrow<< endl; // Print flower name using the following...

  • #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int...

    #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...

  • Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and...

    Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and write the output file; the output file should be the same as the input file, except that it has no e’s. For example, if the input file had the line. Streets meet in the deep. Then the output file would have...

  • Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent...

    Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent debugging * problems. Solve one at a time, uncommenting the next * one only after the previous problem is working correctly. */ public class FindTheErrors { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); /* * Problem 1 Debugging * * This problem is to read in your first name, * last name, and current year and display them in *...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • Please answer in plain text. Determine the C++ output for the following: Assume: int num1 =...

    Please answer in plain text. Determine the C++ output for the following: Assume: int num1 = 25,   num2 = 5,   num3 = 10;          a.    cout<< “ The product is “ << num1 * num3;                 ________________________________________________________ b.     cout<< “ The total of the three numbers is: “ <<(num1+num2+num3);          ________________________________________________________ c.     cout<< “ The total is: “ <<(num1+254.3373);                 _________________________________________________________ d.     cout << num1 << “ +  “    << num2 << “ = ” << num1 + num2;                _________________________________________________________ e.    cout << “ This is how the output...

  • Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division...

    Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division by zero and invalid input. Your program should print Denominator must be nonzero and reprompt for a valid denominator when 0 is entered for a denominator. Please specify what should go in the divisionByZero.h file and the changes made to main.cpp Please provide output. Thank you in advance! main.cpp so far #include <iostream> using namespace std; void addFractions(int num1, int num2, int den1, int...

  • C++ requirements The store number must be of type unsigned int. The sales value must be...

    C++ requirements The store number must be of type unsigned int. The sales value must be of of type long long int. Your program must properly check for end of file. See the section Reading in files below and also see your Gaddis text book for details on reading in file and checking for end of file. Your program must properly open and close all files. Failure to follow the C++ requirements could reduce the points received from passing the...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1...

    SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){    int num1,num2;    cout << "Enter two numbers "<< endl;    cout << "First :";    cin >> num1;    cout << "Second :";    cin >>num2;    int result=num1+num2;    cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result;   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT