Question

Part 1 Describe (in words, not code) a programming scenario where the post-fix and the pre-fix...

Part 1

Describe (in words, not code) a programming scenario where the post-fix and the pre-fix operators impact the value of a variable. Using the same scenario, the value of the variable is different when using the post-fix operation versus the pre-fix operation.

Part 2

Explain static vs non-static methods. Why do we need the keyword static for some methods and not for others?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Part 1:

In programming, prefix operator is used before the variable ( e.g.,++value or --value ) and postfix operator is used after the variable (e.g., value++ or value--).

The use of prefix or postfix operators impact the value of a particular variable very differently in terms of both increment and decrement operators.

In the prefix version (i.e., ++value or --value ), first, the value is incremented/decremented, and then the new value is returned.

In the postfix version (i.e., value++ or value--), the original value is returned first and then the value is incremented/decremented.

For Example, let us see the following code snippet:

int i = 5;

int j = ++i;

cout<<j;

int k = i++;

cout<<i;

a.) Initially, i is set to 5

b.) In the next line, i is incremented to 6 and the new value of i is copied into j and then printed in the next line. So now j is equal to 6.

c.) On the 4th line, i is incremented to 7. The original value of i which is 6 is copied into k. So k is now equal to 6.

But when in the next line we print i, the value shows 7 but j and k will be 6

The same thing happens for postfix and prefix versions of --

Part 2:

A static method belongs to the class, and we do not have to create an instance of the class to access the static method .To create a static method we use the keyword static in front of the method name. A static method can call only other static methods, it cannot call a non-static method. Also, a static can only access static variables, it cannot access instance variables.

On the other hand, a non-static method belongs to an object of the class, and we have to create an instance of the class to access the non-static method. A non-static method can access any static method without creating an instance of the class. A non-static method can access any static variable without creating an instance of the class because the static variable belongs to the class

E.g., static methods:

 

class Calc {

static int product(int x, int y) { //static method

   return x * y;

}

public static void main(String[] args) {

   int ans = Calc.product(5, 3); //method is called directly without creating an instance first

System.out.println(ans); }

}

non-static methods:

 

class Calc {

int product(int x, int y) { //non static method

   return x * y;

}

public static void main(String[] args) {

   Calc calcnew = new Calc();

   // we have to first create an instance of the class Calc to call the non-static method

   int ans = calcnew.product(5, 3); // call the non-static method

   System.out.println(ans);

}

}

The main reason some methods are declared static because of convenience

If we do not make our method/function and variable static, we cannot access that without creating a Object.

But, if we make them static, we do not have to create an object to access that method.

Add a comment
Know the answer?
Add Answer to:
Part 1 Describe (in words, not code) a programming scenario where the post-fix and the pre-fix...
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
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