 |
|

04-25-2010, 09:38 AM
|
 |
Director
|
|
Join Date: Apr 2005
Posts: 4,013
|
|
Fun exam questions
Some of you may know I've been a C programming tutor for a few years.
If you're taking a C programming course at university these are some of the harder types of questions you might find on a final exam:
Q1: Is the following valid C?
Quote:
#include<stdio.h>
int main() {int (*main)(); {printf("hello world\n");
return 0;
}
}
|
Q2: How many times does the following code print "hello\n"?
Quote:
#include <stdio.h>
#define _(x) (;; )
int main() {for _(int i = 0; i < 100; i++) {printf("hello\n");
}
return 0;
}
|
Q3: What does the following code do?
Quote:
|
int main(void);int(*__)()=main;int(**_)()=&__;int main(void){(*_)();}
|
Q4: Why is the following a bad idea?
Q5: What would the following code do?
Quote:
if (*'z' = 010); {printf("%d", *'z');
}
|
Q6: What is the following code equivalent to?
Quote:
char a[10];
int b = 5;
*(a + b) = 5;
|
Q7: What is the type of foo in the following? Is it valid C?
Quote:
typedef void *(*a)(void);
a (*(*foo)(void))(void);
|
Q8: what is the return value of this function?
Quote:
int function(void) {return ({024;007;006;004;001;011});
}
|
Q9: What is the value of c after the following code is executed (assuming c is 2 before execution)?:
|

04-27-2010, 12:46 AM
|
|
Grand Master
|
|
Join Date: Aug 2008
Location: Tree's Pool
Posts: 3,205
|
|
Re: Fun exam questions
Wow, these could definitly be helpful, ive been starting programming and was planning on taking some courses. Thanks for the cheat. :P
|

04-27-2010, 01:46 AM
|
|
Forum Addict
|
|
Join Date: Feb 2007
Posts: 306
|
|
Re: Fun exam questions
c++ would be easier >.>
|

04-29-2010, 03:53 AM
|
 |
Apprentice
|
|
Join Date: Aug 2007
Posts: 998
|
|
Re: Fun exam questions
4) looks like a fork bomb.
5) will print 10? EDIT: 8, it assigned the 010 as an octal number?
It must take a long time to know enough C to answer ones like Q9 in an exam hall 
Last edited by The Fat Controller : 04-29-2010 at 04:11 AM.
|

05-10-2010, 03:30 PM
|
 |
Director
|
|
Join Date: Apr 2005
Posts: 4,013
|
|
Re: Fun exam questions
Quote:
Originally Posted by The Fat Controller
4) looks like a fork bomb.
5) will print 10? EDIT: 8, it assigned the 010 as an octal number?
|
Correct, it is deferencing the address 'z' (the asciicode of z) and assigning it the value '010', which is octal for 8. The branch would always be taken because it is an assignment and an not equality operator; But in this case the if-statement is followed immediately by a semicolon, so the following statement is not part of any branch -- it is instead an independent code-block.
Quote:
It must take a long time to know enough C to answer ones like Q9 in an exam hall
|
You should take in an order of precedence table of operators if you are allowed. This might help you solve a question like 9.
In this particular case I believe the language states the code is ambiguous and undefined.
One might interpret it as:
c += (c++) + (+c); // or
c += (c) + (+(++c));
|

05-11-2010, 06:19 PM
|
 |
Newcomer
|
|
Join Date: May 2010
Location: Santa's House
Posts: 6
|
|
Re: Fun exam questions
1: No
2: 99 times because it's not stated as less than or equal too. ( Also if it would've been a while loop, and the increment sign was in front of the output of the integer! )
3: Not valid
4: Not valid
5: If z is a pointer to 010
6: 0? A is a char with an integer, you couldn't compile
7: Yes
8: Idk
9: C
Idk if all of these are right, those were pretty tricky.
Last edited by XeNoS : 05-11-2010 at 07:31 PM.
|

05-12-2010, 06:39 AM
|
 |
Director
|
|
Join Date: Apr 2005
Posts: 4,013
|
|
Re: Fun exam questions
Quote:
Originally Posted by XeNoS
1: No
|
Wrong.
Quote:
|
2: 99 times because it's not stated as less than or equal too. ( Also if it would've been a while loop, and the increment sign was in front of the output of the integer! )
|
Wrong. Look up #define macros.
Wrong.
Wrong.
Quote:
|
5: If z is a pointer to 010
|
Wrong/Didn't answer the question.
Quote:
|
6: 0? A is a char with an integer, you couldn't compile
|
Wrong.
Wrong/Didn't answer the question.
Wrong.
Wrong.
You scored: 0/9.
|

05-13-2010, 10:51 AM
|
 |
Newcomer
|
|
Join Date: May 2010
Location: Santa's House
Posts: 6
|
|
Re: Fun exam questions
Quote:
Originally Posted by Sythe
Wrong.
Wrong. Look up #define macros.
Wrong.
Wrong.
Wrong/Didn't answer the question.
Wrong.
Wrong/Didn't answer the question.
Wrong.
Wrong.
You scored: 0/9.
|
1: Yes?
O.O
|

05-13-2010, 11:29 AM
|
 |
Director
|
|
Join Date: Apr 2005
Posts: 4,013
|
|
Re: Fun exam questions
Quote:
Originally Posted by XeNoS
1: Yes?
O.O
|
int main() { // define a pointer (called main) to a function that takes unknown arguments and returns an int.
int (*main)();
{ // free standing codeblock.printf("hello world\n");
return 0;
}
}
|

05-14-2010, 08:00 PM
|
 |
Hero
|
|
Join Date: Apr 2005
Posts: 7,068
|
|
Re: Fun exam questions
(I promise I didn't look at the other posts in this thread)
1. Yes.
2. Infinite: you have a for(;  with that macro.
3. I don't know.
4. It would eventually use up all the resources on your system because fork() spawns a new copy of the running executable, right?
5. You have a semicolon after your if statement, so, um, the code block after it doesn't depend on that? Not to mention it's a single = sign not ==, so the if statement has no real test expression anyway. In the off-chance that you did this intentionally, I don't know what it's supposed to do.
6. I give up.
7. Function of type **void.
8. Nothing, because those should be commas, not semicolons. EDIT: VS2008 doesn't compile that.
9. c += c++ +(+c) = 2 += 2 + 2 = 6? I thought it was 7 at first but then I realized that if it was (c++) (++c) there would be no mathematical operation.
Last edited by SMR : 05-14-2010 at 08:40 PM.
|

05-17-2010, 07:39 AM
|
 |
Director
|
|
Join Date: Apr 2005
Posts: 4,013
|
|
Re: Fun exam questions
Quote:
Originally Posted by SMR
(I promise I didn't look at the other posts in this thread)
1. Yes.
|
Correct.
Quote:
2. Infinite: you have a for(; with that macro.
|
Correct.
Wrong/Didn't answer.
int main(void); //Prototype for main
int(*__)()=main; // Function pointer called __ which points at main
int(**_)()=&__; // Function pointer pointer called _ which points at __
int main(void){
(*_)(); // dereference _, and call the resulting function pointer. (Infinite recursion.)
}
Quote:
|
4. It would eventually use up all the resources on your system because fork() spawns a new copy of the running executable, right?
|
Correct, fork bomb.
Quote:
|
5. You have a semicolon after your if statement, so, um, the code block after it doesn't depend on that? Not to mention it's a single = sign not ==, so the if statement has no real test expression anyway. In the off-chance that you did this intentionally, I don't know what it's supposed to do.
|
Half mark, didn't answer the question but got halfway there.
(It is intentional, and it's good you picked it up.)
The code dereferences the ascii-code for 'z' and sets the memory there to octal 010. (decimal 8). It then proceeds to the unconnected codeblock, and prints the memory at the address which is the ascii-code of 'z', which we just set to 010, which is 8. So it prints 8.
Wrong/didn't answer the question.
The code is equivalent to the following:
char a[10];
int b = 5;
a[b] = 5;
or
char a[10];
a[5] = 5;
Quote:
|
7. Function of type **void.
|
Wrong, it's a function pointer. I won't reveal what type of function pointer.
Quote:
|
8. Nothing, because those should be commas, not semicolons. EDIT: VS2008 doesn't compile that.
|
Wrong.
The last value or variable in a code-block is it's value if used in an expression. This is actually valid C but not all compilers support it. That function returns the octal value 011.
Quote:
|
9. c += c++ +(+c) = 2 += 2 + 2 = 6? I thought it was 7 at first but then I realized that if it was (c++) (++c) there would be no mathematical operation.
|
It's ambiguous, it depends on the compiler. I'll give you this mark.
Your score: 4.5/9.
|

05-17-2010, 11:47 PM
|
 |
Hero
|
|
Join Date: Apr 2005
Posts: 7,068
|
|
Re: Fun exam questions
Quote:
char a[10];
int b = 5;
a[b] = 5;
or
char a[10];
a[5] = 5;
|
I can't believe I didn't see that one right away. Oh well...
|

05-24-2010, 10:29 AM
|
|
Member
BANNED
|
|
Join Date: Apr 2010
Posts: 91
|
|
Re: Fun exam questions
Thankyou! Im currently doing computer programming, year 12 and was looking as of what university exams would look like. One quick question, do you think by the time im in university they will still teach c? or just c++ and other?
|

05-25-2010, 05:19 PM
|
 |
Director
|
|
Join Date: Apr 2005
Posts: 4,013
|
|
Re: Fun exam questions
Quote:
Originally Posted by vbpro
Thankyou! Im currently doing computer programming, year 12 and was looking as of what university exams would look like. One quick question, do you think by the time im in university they will still teach c? or just c++ and other?
|
Any respectable computer science or IT degree will contain at minimum one course in each C and Java programming. You might also get your hands on C++ depending on how good the university is.
|

07-11-2010, 11:21 PM
|
|
Newcomer
|
|
Join Date: Jul 2010
Posts: 3
|
|
Re: Fun exam questions
This looks very difficult not even sure if i wanna major in computer science anymore :/
|

07-11-2010, 11:56 PM
|
|
Guru
|
|
Join Date: Jun 2005
Location: Canada
Posts: 1,844
|
|
Re: Fun exam questions
Quote:
Originally Posted by halo2ownage8
This looks very difficult not even sure if i wanna major in computer science anymore :/
|
Don't let the syntax scare you off. Once you start learning the language and start understanding the basics and getting into the harder stuff, this will be nothing.
|

09-17-2010, 02:40 AM
|
 |
Active Member
|
|
Join Date: Jan 2010
Posts: 230
|
|
Re: Fun exam questions
Pure giberish... What is the Owner talking about?
|

09-18-2010, 01:30 AM
|
|
Grand Master
BANNED
|
|
Join Date: Feb 2008
Location: Las Vegas
Posts: 2,813
|
|
Re: Fun exam questions
Quote:
Originally Posted by clawed
Pure giberish... What is the Owner talking about?
|
If you don't know a thing about C, don't post here.
|

11-18-2010, 07:05 PM
|
|
Newcomer
|
|
Join Date: Nov 2010
Posts: 22
|
|
Re: Fun exam questions
I really need to stop slacking off and finally learn this.
|

11-19-2010, 02:57 PM
|
 |
Active Member
|
|
Join Date: Nov 2010
Location: US
Posts: 179
|
|
Re: Fun exam questions
Quote:
Originally Posted by Blunted xD
I really need to stop slacking off and finally learn this.
|
Make sure you do not waste too much time for C because it is quite obsolete nowadays.
|
 |
|
|