What is the final number output for following code snippet


Assignment

Part 1

Question 1. A variable declared before and outside all function blocks _____.
is visible only in main
is visible to all functions
is visible to all functions except main
is not visible to any functions

Question 2. What is the final number output for the following code snippet?
for (i = 1 ; i<= 20 ; i = i + 2);
cout<< " " < 10
19
21
Illegal set-up of for loop parameters

Question 3. If firstNameand lastNameare string object variables, which statement can be used to combine (append or concatenate) the two into a single string variable?
fullName = firstName + lastName;
fullName = firstName, lastName;
fullName = firstName&lastName;
fullName = firstName&&lastName;

Question 4. C++ selection statements include _____.

for, while, and do-while
coutand cin
if, if-else, if-else if, and switch
#include and using namespace std;

Question 5. Which of the following expressions is correct if you want to end a while loop when the character variable keepgoingis anything other than character y in either uppercase or lowercase?
while (keepgoing == "y" || keepgoing == "Y")
while (keepgoing == 'y' && keepgoing == 'Y')
while (keepgoing == "y" | | keepgoing == "Y")
while (keepgoing == 'y' | | keepgoing == 'Y')

Question 6. Given the following code fragment, what is the data type of roster[9] .name?

struct student
{
string name;
double gpa;
};
student thisStudent;
student roster[50];
string
const pointer to student
student
double

Question 7. Which type of error does the following code fragment cause?
constint MAX = 500;
int main (void)
{
int foo [MAX];
for (inti = 0; i<= MAX; i++)
{
foo [i] = i * 2;
}
Compiler, due to out-of-bounds array subscript
Run-time, due to out-of-bounds array subscript
Compiler, due to invalid array initialization
None of the above (t does not create any type of error)

Question 8. What is the data type of bullpen[3].era?
struct pitcher
{
string name;
double era;
};
pitcher p1;
pitcher bullpen[10];
string
double
pitcher
const pointer to pitcher

Question 9. What is the output of the following code?
void func(int x)
{
x = x * 2;
}
int main( )
{
int x = 10;
func(x);
cout<< x < }
20
30
10
5

Question 10. If the function square(x) returns the square of x, then x should be passed by _____.
value so it is protected (cannot be changed)
reference so it is protected (cannot be changed)
pointer so it is protected (cannot be changed)
None of the above (x cannot be protected; square can change the value of x regardless of how it is passed)

Question 11. When a variable is passed by reference, _____.
the variable can be changed by the calling and the called function
the parameter name is an alias of the variable passed
the called function can change the value of the variable in the calling program
All of the above

Part 2

Question 1. Given the following class definition and lines of code, Line 1 in main is a call to what?
class Distance
{
private:
int feet;
double inches;
public:
Distance( );
Distance(intinitFt, double initIn);
void setFeet(intfeetIn);
void setInches(double inchesIn);
intgetFeet() const;
double getInches( ) const;
};
int main( )
{
Distance d1; //Line 1
constint MAX = 100; //Line 2
Distance list[MAX]; //Line 3
Distance d2(1, 2.3); //Line 4
Distance * pDist; //Line 5
d1.feet = 5; //Line 6
// etc. - assume the remaining code is correct
}
The 0-argument Distance constructor
The 2-argument, int, double, Distance constructor
The 2-argument, double, int, Distance constructor
The 1-argument, int, Distance constructor

Question 2. Composition is typically an example of _____.
a "has a" relationship
an "is a" relationship
a "uses a" relationship
an "is used" relationship

Question 3. Given the following class definition and lines of code, what, if anything, is wrong with Line 6 in main?
class Distance
{
private:
int feet;
double inches;
public:
Distance( );
Distance(intinitFt, double initIn);
void setFeet(intfeetIn);
void setInches(double inchesIn);
intgetFeet() const;
double getInches( ) const;
};
int main( )
{
Distance d1; //Line 1
constint MAX = 100; //Line 2
Distance list [MAX]; //Line 3
Distance d2(1, 2.3); //Line 4
Distance * pDist; //Line 5
d1.feet = 5; //Line 6
// etc. - assume the remaining code is correct
}
It will not compile because feet is private and cannot be directly accessed.
Distance d1; should be changed to int d1;.
The ::d1.feet = 5; should be changed to d1(feet) = 5;.
It will compile but will cause a run-time error because d1.feet has not been declared.

Question 4. Which of the following is called automatically each time an object is created?
Compiler
Builder
Constructor
Destructor

Question 5. Variables defined to be of a user-declared class are referred to as _____.
attributes
member variables
primitive variables
objects

Question 6. What is the data type of pDist for the code snippet below?
Distance * pDIST;
Distance
Const pointer to Distance
Pointer to Distance
None of the above

Question 7. Given the following definitions, select the statement that is illegal.
int * iptr;
double * dptr;
int j = 10;
double d = 10.0;
iptr = &j;
dptr = &d;
iptr = 0;
dptr = &j;

Question 8. Given the definition of some class called Employee, which of the following correctly dynamically allocates an array of 20 Employee objects?
Employee * myData = new Employee[20];
Employee myData[20] = new Employee[20];
Employee = myData[20];
Employee array = new Employee[20];

Question 9. In order to be able to use the JFrame, JPanel, and JButton classes, you must _____.
include the Swing header file ("Swing.h")
import the Swing class
import javax.Swing.*;
import java.awt.*;

Question 10. In the following Java code, the tfieldvariable is defined to be a JTestField. What data type must the input variable be declared as?
Input = tfield.getText( );
Double
Integer
String
String [ ]

Question 11. Which of the following creates a multiline display which is user editable?
JLabel display = new JLabel("Enter text");
JTextField display = new JTextField("Enter text");
JTextArea display = new JTextArea(20, 20);
JScrollPane display = new JScrollPane( );

Question 12. What range of numbers does the following expression generate?
Intnum = 1 + (int) (Math.Random ( ) * 100);
Minimum is 0, maximum is 100
Minimum is 1, maximum is 101
Minimum is 1, maximum is 100
Minimum is 1, maximum is 101

Question 13. What is the result of the following code?
JFrame frame = new JFrame( );
frame.add(new JButton("One"), BorderLayout.NORTH);
frame.add(new JButton("Two"), BorderLayout.NORTH);
frame.setVisible(true);
Two buttons are added side by side at the top of the display.
Button One was added first, so it is the only button at the top of the display.
Button Two was added last, so it is the only button at the top of the display.
The Java compiler will not allow you to add two buttons to the same area.

Question 14. What does the following Java code do?
JPanel pane = new JPanel( );
pane.setLayout( new gridLayout(2, 3) );
Creates a panel that will organize its components into five distinct cells
Creates a panel that displays a grid containing the numbers 2 and 3
Creates a panel that will organize its components into two rows of three columns
None of the above

Solution Preview :

Prepared by a verified Expert
Programming Languages: What is the final number output for following code snippet
Reference No:- TGS02237441

Now Priced at $50 (50% Discount)

Recommended (99%)

Rated (4.3/5)

©TutorsGlobe All rights reserved 2022-2023.