Sythe.Org Forums     Register     FAQ     Members List     Calendar     Mark Forums Read    
 
Sythe.Org Forums  
   Runescape Gold

Sythe.org — A Virtual Goods Trading Hub

Make real cash! buying and selling in-game items.

We have a no-scam policy.

You can make thousands playing your favourite games here at Sythe.org.

Just sign up an account and follow the rules!


Take me to

Runescape Markets

Other Game Markets

Support Center

Register an Account

Close
Fun exam questions
Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 04-25-2010, 09:38 AM
Sythe's Avatar
Director
 
Join Date: Apr 2005
Posts: 4,013
Yellow Party Hat 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?
Quote:
while(1) fork();
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)?:
Quote:
c += c++++c;
Reply With Quote
  #2  
Old 04-27-2010, 12:46 AM
Grand Master
 
Join Date: Aug 2008
Location: Tree's Pool
Posts: 3,205
Send a message via MSN to Lukef555
Default 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
Reply With Quote
  #3  
Old 04-27-2010, 01:46 AM
Forum Addict
 
Join Date: Feb 2007
Posts: 306
Default Re: Fun exam questions

c++ would be easier >.>
Reply With Quote
  #4  
Old 04-29-2010, 03:53 AM
The Fat Controller's Avatar
Apprentice
 
Join Date: Aug 2007
Posts: 998
Default 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.
Reply With Quote
  #5  
Old 05-10-2010, 03:30 PM
Sythe's Avatar
Director
 
Join Date: Apr 2005
Posts: 4,013
Default Re: Fun exam questions

Quote:
Originally Posted by The Fat Controller View Post
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));
Reply With Quote
  #6  
Old 05-11-2010, 06:19 PM
XeNoS's Avatar
Newcomer
 
Join Date: May 2010
Location: Santa's House
Posts: 6
Send a message via AIM to XeNoS Send a message via MSN to XeNoS Send a message via Yahoo to XeNoS Send a message via Skype™ to XeNoS
Default 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.
Reply With Quote
  #7  
Old 05-12-2010, 06:39 AM
Sythe's Avatar
Director
 
Join Date: Apr 2005
Posts: 4,013
Default Re: Fun exam questions

Quote:
Originally Posted by XeNoS View Post
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.

Quote:
3: Not valid
Wrong.

Quote:
4: Not valid
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.

Quote:
7: Yes
Wrong/Didn't answer the question.

Quote:
8: Idk
Wrong.

Quote:
9: C
Wrong.

You scored: 0/9.
Reply With Quote
  #8  
Old 05-13-2010, 10:51 AM
XeNoS's Avatar
Newcomer
 
Join Date: May 2010
Location: Santa's House
Posts: 6
Send a message via AIM to XeNoS Send a message via MSN to XeNoS Send a message via Yahoo to XeNoS Send a message via Skype™ to XeNoS
Default Re: Fun exam questions

Quote:
Originally Posted by Sythe View Post
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
Reply With Quote
  #9  
Old 05-13-2010, 11:29 AM
Sythe's Avatar
Director
 
Join Date: Apr 2005
Posts: 4,013
Default Re: Fun exam questions

Quote:
Originally Posted by XeNoS View Post
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;
}
}
Reply With Quote
  #10  
Old 05-14-2010, 08:00 PM
Govind's Avatar
Hero
Highly Respected Highly Trusted Sythe Verified User
 
Join Date: Apr 2005
Posts: 7,068
Default 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.
Reply With Quote
  #11  
Old 05-17-2010, 07:39 AM
Sythe's Avatar
Director
 
Join Date: Apr 2005
Posts: 4,013
Default Re: Fun exam questions

Quote:
Originally Posted by SMR View Post
(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.
Quote:
3. I don't know.
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.

Quote:
6. I give up.
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.
Reply With Quote
  #12  
Old 05-17-2010, 11:47 PM
Govind's Avatar
Hero
Highly Respected Highly Trusted Sythe Verified User
 
Join Date: Apr 2005
Posts: 7,068
Default 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...
Reply With Quote
  #13  
Old 05-24-2010, 10:29 AM
Member
BANNED
 
Join Date: Apr 2010
Posts: 91
Default 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?
Reply With Quote
  #14  
Old 05-25-2010, 05:19 PM
Sythe's Avatar
Director
 
Join Date: Apr 2005
Posts: 4,013
Default Re: Fun exam questions

Quote:
Originally Posted by vbpro View Post
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.
Reply With Quote
  #15  
Old 07-11-2010, 11:21 PM
Newcomer
 
Join Date: Jul 2010
Posts: 3
Default Re: Fun exam questions

This looks very difficult not even sure if i wanna major in computer science anymore :/
Reply With Quote
  #16  
Old 07-11-2010, 11:56 PM
Guru
 
Join Date: Jun 2005
Location: Canada
Posts: 1,844
Default Re: Fun exam questions

Quote:
Originally Posted by halo2ownage8 View Post
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.
__________________

Reply With Quote
  #17  
Old 09-17-2010, 02:40 AM
clawed's Avatar
Active Member
 
Join Date: Jan 2010
Posts: 230
Send a message via MSN to clawed Send a message via Skype™ to clawed
Default Re: Fun exam questions

Pure giberish... What is the Owner talking about?
Reply With Quote
  #18  
Old 09-18-2010, 01:30 AM
Grand Master
BANNED
 
Join Date: Feb 2008
Location: Las Vegas
Posts: 2,813
Send a message via MSN to Kleenex
Default Re: Fun exam questions

Quote:
Originally Posted by clawed View Post
Pure giberish... What is the Owner talking about?
If you don't know a thing about C, don't post here.
Reply With Quote
  #19  
Old 11-18-2010, 07:05 PM
Newcomer
 
Join Date: Nov 2010
Posts: 22
Default Re: Fun exam questions

I really need to stop slacking off and finally learn this.
Reply With Quote
  #20  
Old 11-19-2010, 02:57 PM
blahbleh's Avatar
Active Member
 
Join Date: Nov 2010
Location: US
Posts: 179
Send a message via MSN to blahbleh
Default Re: Fun exam questions

Quote:
Originally Posted by Blunted xD View Post
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.
__________________
My Vouches
  • 5 Good Trades.
  • 0 Bad Trades.
  • $70 in Total Value.
Reply With Quote
Reply



Cheap RS Gold Store  Runescape Gold

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

All times are GMT +1. The time now is 09:12 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.6.1