Question

1) Short Answer: a) Explain why a reference cannot be NULL (as opposed to a pointer which can be NULL). (2 points) b) Argue h

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • a) Pointers is null which means it points to nothing.variables can be declared as a reference.Its like another name of a variable.
    Example - int x = 10; this is a variable
    while  int &ref = x ; this is a reference .
    If we change the value of reference, value of variable also changes,
    ex - ref=20; this means now x also contains 20.
    Reference cannot be changed once created, and you can not create a reference of something which is not even there i.e. null, hence reference cannot be null, but pointers can, this is the reason we use pointers to implement linked list and not references.
  • b) when a variable is declared static the memory allocated to it is gets allocated to it for the lifetime of the program.Even if you call function multiple times , space allocated to x is same which is 599.It always restores it previous state no matter how many times you change it. In above example when cout<<emapleb() was done, it restored it orignal state which was 599 and not 595.
  • Boxing is converting a value type to type object,and unboxing is converting object type to type value.
    example -
    int i = 3;
    object o = i; // Box converting int type to object
    int j = (int) o; //UnBoxing converting object to int type
  • Functions are used to remove code duplicacy, suppose you want to check if 10 numbers are prime, if functions are not used you need to check each and every number 10 times,whether it is prime or not, which increases number of lines of code ,and also decreases code readability, instead we can create a function checkPrime(int n) which accepts a number and returns true/false checking if a number is prime or not. Hence functions are really important to improve code readability.
Add a comment
Know the answer?
Add Answer to:
1) Short Answer: a) Explain why a reference cannot be NULL (as opposed to a pointer...
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
  • TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes...

    TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes P1 to point to the memory location 1001. ANSWER: T 2. When you return a dynamic array to the freestore, you must include the number of elements in the array 3. You can assign an array to a pointer variable. 4. The size of dynamic arrays must be declared at compile time. 5. int *pl; declares a static variable. ANSWER: F ANSWER: F ANSWER:...

  • MUST USE C++ PLEASE READ THE QUESTION CAREFULLY!! ADD COMMENTS/EXPLAIN Question 1: Template Function with Pointer...

    MUST USE C++ PLEASE READ THE QUESTION CAREFULLY!! ADD COMMENTS/EXPLAIN Question 1: Template Function with Pointer Arguments Design and implement a template function, called swapt(T *p1, T *p2), which takes two parameters with the same generic data type. The function swaps the values of these two parameters. Test this function in the following main( ) : int main( ) {             int x = 30,   y = 40;             swapt(&x, &y);             cout << “x = “ << x...

  • 6. (Short answer) The C++ code below is very close to working, but, as written, won't...

    6. (Short answer) The C++ code below is very close to working, but, as written, won't compile. When trying to compile, the compiler generates a number of errors similar to "cout' was not declared in this scope." Respond to the prompts below about the code.(20 points, 5 points each) #include <iostream> void FindMax(int a, int b, int& max) { if (a > b) max = a; else { max = b; } int main() { int num1 = 0; int...

  • Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue....

    Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ.   Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...

  • Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In...

    Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In C++ there are commands that let the user request a chunk of memory from the operating system, and use this memory to store data. There are also commands to return memory back to the O/S when the program is finished using the data. In this lab, we will explore some of the things that can go wrong when using dynamic memory and discuss how...

  • Part 5: C++ Programming (25/100) 13) (5 marks) Explain the difference between a call by value...

    Part 5: C++ Programming (25/100) 13) (5 marks) Explain the difference between a call by value and a call by reference. 14) (5 marks) What value of y will be displayed when the following ++ program is run: tinclude <iostream int main) double x 3.0, y: y1/3 x16.0) std::cout <<y<"n" return (0): Show the details step by step as how the computer would perform the calculation and justify your resuits. Also, what will the data type of y be? For...

  • 1. Answer questions (a) through (d) for the mutant on line 6 in the method sum(). Sum values in an array Oparam x array to sum *Oreturn sum of values in x Othrows NullPointerException if x is null pu...

    1. Answer questions (a) through (d) for the mutant on line 6 in the method sum(). Sum values in an array Oparam x array to sum *Oreturn sum of values in x Othrows NullPointerException if x is null public static int sum(int[] x) 2. 3. int -0 4. for (int i-0; ǐ < x·length; İ++) } s=s+x[1]; 6 6 11s-s-x(4]://AOR 7. 8 return s (a) If possible, find test inputs that do not reach the mutant. (2 pts.) (b) If...

  • C++ 1. Please use attached script for your reference to create the node structure and a...

    C++ 1. Please use attached script for your reference to create the node structure and a linked list class, say LinkedList, that has the following basic member methods:     constructor, destructor//IMPORTANT, display(), add_node(). 2. Please implement the following additional member methods:     Please feel free to change T with any data type you'd like to use for your node stricture's data type. -- addFirst(T data) // Adds an node with data at the beginning of the list -- pop() //...

  • C++ 1. Please use attached script for your reference to create the node structure and a...

    C++ 1. Please use attached script for your reference to create the node structure and a linked list class, say LinkedList, that has the following basic member methods:     constructor, destructor//IMPORTANT, display(), add_node(). 2. Please implement the following additional member methods:     Please feel free to change T with any data type you'd like to use for your node stricture's data type. -- addFirst(T data) // Adds an node with data at the beginning of the list -- pop() //...

  • Please answer all the questions thank you 1) (Classes – 20 Points) Consider the following class...

    Please answer all the questions thank you 1) (Classes – 20 Points) Consider the following class declaration for Time to complete the questions below: DO NOT WRITE MORE THAN ASKED FOR. class Time private: int hours; int minutes; public: Time(); Time (int , int m = 0); void addMin(int m); void addHr(int h); void reset(int h = 0, int m = 0); Time operator+(const Time & t) const; Time operator-(const Time & t) const; Time operator*(double n) const; friend Time...

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