Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong C++ answer(?s) #5306

Merged
merged 2 commits into from
Jan 8, 2023
Merged

Fix wrong C++ answer(?s) #5306

merged 2 commits into from
Jan 8, 2023

Conversation

dehre
Copy link
Contributor

@dehre dehre commented Jan 6, 2023

PR Checklist

This PR is ready for review and meets the requirements set out
in Suggestion how to contribute

DOD

  • I have added new quiz{'s}
  • I have added new reference link{'s}
  • I have made small correction/improvements

Changes / Instructions

Question 28: What is the value of x after running this code?

It seems to me that either the question or the answer is wrong.
Compiling and running this snippet with both clang and gcc prints -3:

#include <iostream>
int main()
{
    int x = 10, a = -3;
    x = +a;
    std::cout << x << '\n';
    return 0;
}

The original answer, -7, would have been correct given this snippet instead:

int x = 10, a = -3;
x += a;

@unikdahal I've seen your recent PR here, what do you think? Which compiler did you use?

Question 44: What is the result from executing this code snippet?

bool x=true, y=false;

if (~x || y) {
    /*part A*/
} else {
    /*part B*/
}

Original answer: Part A executes because the expression (~x || y) always results in true if y==false.

Well, it's correct that part A executes, but in my opinion the correct answer is: Part A executes because ~x is not zero, meaning true.

Assuming the boolean value is represented using one byte, true becomes 00000001, and flipping its bits you get 11111110, which is a non-zero value.
Therefore the if-condition will always evaluate to true, no matter the value of y.
To confirm this theory, if you compile using the -Wbool-operation flag, with clang you get a warning stating: "bitwise negation of a boolean expression always evaluates to 'true'".

@golamrabbiazad you recently updated this answer, what do you think?

Question 34: What is the output of this code?

#include <cstdio>
using namespace std;

int main(){
    char c = 255;
    if(c>10)
        printf("c = %i, which is greater than 10", c);
    else
        printf("c = %i, which is less than 10", c);
    return 0;
}

Possible answers:

  • c = -1, which is less than 10
  • c = 255, which is greater than 10
  • c = -1, which is greater than 10
  • c = 255, which is less than 10

The result is correct on my machine too, but, according to the standard, char could be either signed or unsigned; in the latter case, the answer would have been: "c = -1, which is greater than 10".
From the reference:

The signedness of char depends on the compiler and the target platform: the defaults for ARM and PowerPC are typically unsigned, the defaults for x86 and x64 are typically signed.

So, either I checked something wrong, or LinkedIn should update its question.
Perhaps some veteran C++ developer can provide feedback?

@dehre dehre marked this pull request as ready for review January 6, 2023 10:45
@golamrabbiazad
Copy link
Contributor

golamrabbiazad commented Jan 6, 2023

@dehre On the 44th question, You are absolutely right and I agree with that explanation.

Actually, the first time I thought that answer would be either A or C and I don't have a strong explanation for bit shifting to mark C as the answer. I go with the flow and pretty understandable answer A and I looked at the y value given as a false.
If the left part always returns true then doesn't matter if the right part is true or false which is super understandable.

So, the right answer should be C. 👍

Please, add this explanation as a reference to this question so that I can submit a review.

@dehre
Copy link
Contributor Author

dehre commented Jan 6, 2023

Please, add this explanation as a reference to this question so that I can submit a review.

I'm not sure I should.
Looking at the other answers, in this and other quizzes, rarely there's any verbose explanation.
Sure, there's some link here and there, but that's it.

An exception would be question 34 just because none of the answers seem to be totally correct.

@golamrabbiazad
Copy link
Contributor

golamrabbiazad commented Jan 6, 2023

Alright, coming to the 34th question.

Yes, the char type could be signed or unsigned.

the defaults for ARM and PowerPC are typically unsigned

Yes, there are some compilers on some platforms that may define the char type as unsigned by default in order to optimize code for certain types of applications.

It will be a long conversation if we go with that statement.

By default, the char type is signed on most systems. The char type is equivalent to the signed char type so, the c variable will be treated as a signed integer type, and it stored a negative value as -1 because of its range.

if it is unsigned then it would have been explicitly provided using unsigned char.

I Hope, I clarified this.

@dehre
Copy link
Contributor Author

dehre commented Jan 8, 2023

Exactly, -1 is the correct answer most of the times.
Question is, should we make it clear, or just leave it out?
On the one hand, it would make the answer (a bit) more correct, or informative at least.
On the other, this document isn't a tutorial, there are barely some references here and there.

@Ebazhanov
Copy link
Owner

Alright, coming to the 34th question.

Yes, the char type could be signed or unsigned.

the defaults for ARM and PowerPC are typically unsigned

Yes, there are some compilers on some platforms that may define the char type as unsigned by default in order to optimize code for certain types of applications.

It will be a long conversation if we go with that statement.

By default, the char type is signed on most systems. The char type is equivalent to the signed char type so, the c variable will be treated as a signed integer type, and it stored a negative value as -1 because of its range.

if it is unsigned then it would have been explicitly provided using unsigned char.

I Hope, I clarified this.

thanks for your comments @golamrabbiazad I have added you to the table in front of C++.

Copy link
Owner

@Ebazhanov Ebazhanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @dehre for your valuable contribution! Great job 👍

@Ebazhanov Ebazhanov merged commit 5085701 into Ebazhanov:main Jan 8, 2023
@Ebazhanov
Copy link
Owner

@all-contributors please add @dehre for design, code

@allcontributors
Copy link
Contributor

@Ebazhanov

I've put up a pull request to add @dehre! 🎉

@golamrabbiazad
Copy link
Contributor

Question is, should we make it clear, or just leave it out?

I think, what we discussed, is enough for the clearness. It's not that deep we are thinking about, it's a LinkedIn test.

Nice to have a conversation with you, thanks!

On the one hand, it would make the answer (a bit) more correct, or informative at least.

Yes, I wish 💯

@dehre dehre deleted the cpp-quiz branch January 9, 2023 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants