Jump to content


Dare to test your C++ knowledge here?


31 replies to this topic

#1 bladder

    DevMaster Staff

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 28 October 2004 - 12:09 PM

Try and do this without referencing anything to see how much you really know about c++. 18 questions all together. none of them have answers more the a line or two.

NOTE: If you don't want any of the answers spoilt, then answer them in a notepad or something before scrolling through the replies.

Q 000) What's the difference between 'char* = "blah"' and 'char[] = "blah"'?

Q 001) What does koenig look-up allow us to do?

Q 002) Give an example of partial ordering of function templates.

Q 003) What is placement new?

Q 004) When is placement delete used?

Q 005) What's the maximum value x can contain?

 unsigned int x:5;

Q 006) What will the following display?

 short x = -32768;
 short y = -x;
 if( x != y ) std::cout << "!="<< std::endl;
 else std::cout<< "=="<< std::endl;

Q 007) When does the following evaluate to true?

 if( x == x == x )

Q 008) When does the following evaluate to true?

 if( x != x != x )

Q 009) What will be the value of x?

 int x = 2;
 x += x++;

Q 010) What does the follwoing print?

 std::cout<< ( 4 >= 3 >= 2 )<< std::endl;
 std::cout<< ( 2 >= 1 >= 0 )<< std::endl;

Q 011) How do you invoke the ctor and dtor explicitly?

Q 012) Will std::vector always have contigious memory?

Q 013) What is template specialization and partial specialization?

Q 014) Rewrite the following function so that the following conditions are satisfied:
	A) the multiplication operator ('*') is not used.
	:) the division operator ('/') is not used.
	C) the function does not make use of loops.
	D) And no using assembly either

 int MultiplyBy321(int val)
 {
  int res;
  res = val * 321;
  return res;
 }

Q 015) Determine if a given type T is const or not at compile time.

Q 016) What is the purpose of inheritance?

Q 017) How would you load a text file on disk into memory?

Q 018) What does the following macro do?

 #define macro(x, y) (int)(&((x*)0)->y)

REMEMBER: DO NOT scroll past this point if you dont want to see the answers to some questions.

#2 Nick

    Senior Member

  • Members
  • PipPipPipPip
  • 1227 posts
  • LocationOttawa, Ontario, Canada

Posted 28 October 2004 - 12:48 PM

What can we win? :D Should we send you a private message or spoil the answers?

#3 bladder

    DevMaster Staff

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 28 October 2004 - 01:27 PM

what you win is the satisfaction that you are one with the force...err...i mean with c++. And yeah by all means just post the answers the them right here. Ill edit my above post and tell people not to scroll down if they don't want to see any answers.

#4 Mattias Gustavsson

    Senior Member

  • Members
  • PipPipPipPip
  • 413 posts

Posted 28 October 2004 - 02:04 PM

bladder said:

Q 018) What does the following macro do?

  #define macro(x, y) (int)(&((x*)0)->y)


Make the code harder to read? :rolleyes:
  • www.mattiasgustavsson.com - My blog and current projects
  • www.rivtind.com - My Fantasy world and isometric RPG engine
  • www.pixieuniversity.com - My Software 2D Game Engine

#5 pecina

    New Member

  • Members
  • Pip
  • 1 posts

Posted 28 October 2004 - 02:52 PM

Mattias Gustavsson said:

bladder said:

Q 018) What does the following macro do?

  #define macro(x, y) (int)(&((x*)0)->y)


Make the code harder to read? :)

View Post


Returns an offset of var from begging of class.

struct vector {
float x, y
};

macro(vector, x) will return 0 and
macro(vector, y) will return 4

#6 Ed Mack

    Senior Member

  • Members
  • PipPipPipPip
  • 1239 posts

Posted 28 October 2004 - 03:32 PM

Q 014) Rewrite the following function so that the following conditions are satisfied:
	A) the multiplication operator ('*') is not used.
	B) the division operator ('/') is not used.
	C) the function does not make use of loops.
	D) And no using assembly either

 int MultiplyBy321(int val)
 {
  int res;
  res = val * 321;
  return res;

Is this an acceptable answer?
template <unsigned N> struct _MultiplyBy321
{
 enum { Answer = 321 + _MultiplyBy321<N-1>::Answer };
};
                                        
template <> struct _MultiplyBy321 <1>
{
 enum { Answer = 321 };
};
                                        
/* Somewhere else: */
#define MultiplyBy321(x) _MultiplyBy321<x>::Answer
(I had to use reference when it wouldn't compile, sorry :( )

#7 Mihail121

    Senior Member

  • Members
  • PipPipPipPip
  • 1059 posts

Posted 28 October 2004 - 04:21 PM

bladder said:

Q 014) Rewrite the following function so that the following conditions are satisfied:
	A) the multiplication operator ('*') is not used.
	B) the division operator ('/') is not used.
	C) the function does not make use of loops.
	D) And no using assembly either

 int MultiplyBy321(int val)
 {
  int res;
  res = val * 321;
  return res;
 }

REMEMBER: DO NOT scroll past this point if you dont want to see the answers to some questions.

View Post


Ok, i have a recursive add solution but someone might say it's a form of loop. Anyway, here it goes:


int MulBy321(int val) {
  return RecAdd(321, val);
}

int RecAdd(int num, int val) {
  if (!num)
    return 0;
  
  return val + RecAdd(num - 1, val);
}

void main() {
 printf("9 * 321 = %d", MulBy321(9));
}



#8 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 28 October 2004 - 04:39 PM

321 = 256 + 64 + 1 =>

x*321 = x*256 + x*64 + x

=>

x*321 = x<<8 + x<<5 + x

thats the "lowlevel answer"
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#9 Nick

    Senior Member

  • Members
  • PipPipPipPip
  • 1227 posts
  • LocationOttawa, Ontario, Canada

Posted 28 October 2004 - 04:50 PM

bladder said:

Q 001) What does koenig look-up allow us to do?
Nothing that we couldn't before. :cool:

Koenig was just lazy... that's why we love him!

#10 Mihail121

    Senior Member

  • Members
  • PipPipPipPip
  • 1059 posts

Posted 28 October 2004 - 04:51 PM

Isn't shift exactly kind of multiply operator? Oh damn, if you are going to give questions be more specific, ok?

#11 Nick

    Senior Member

  • Members
  • PipPipPipPip
  • 1227 posts
  • LocationOttawa, Ontario, Canada

Posted 28 October 2004 - 05:03 PM

bladder said:

Q 006) What will the following display?

 short x = -32768;
 short y = -x;
 if( x != y ) std::cout << "!="<< std::endl;
 else std::cout<< "=="<< std::endl;
-32768 == -32768

It's the biggest negative value a short can hold. But 32767 is the biggest positive number so 32768 has no positive. The result can be found by negating all bits of 0x8000 and adding 1 (the equivalent of a negation). Which is again 0x8000 or -32768.

#12 Nick

    Senior Member

  • Members
  • PipPipPipPip
  • 1227 posts
  • LocationOttawa, Ontario, Canada

Posted 28 October 2004 - 05:16 PM

bladder said:

Q 011) How do you invoke the ctor and dtor explicitly?
Forget all about Koenig. :D

Here's an extra question: How to get a pointer to a constructor? Don't take the question too literal, solve the problem.

#13 bladder

    DevMaster Staff

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 28 October 2004 - 07:17 PM

Ed Mack said:

Is this an acceptable answer?
(I had to use reference when it wouldn't compile, sorry :( )

Yeah I suppose that's acceptable. Not really a function though, it's a macro. Since you were already on the recursive bandwagon you might as well just have done this:

int MultiplyBy321( int x )
{
  if( x == 1 ) return 321;
  else return MultiplyBy321(x-1) + 321;
}

But templates are always fun eh ;)


Mihail121 said:

Ok, i have a recursive add solution but someone might say it's a form of loop. Anyway, here it goes:

Yeah that works. loops would just include while/for/do/(i suppose some may say goto and label would also be a loop, but whatever...).

davepermen said:

321 = 256 + 64 + 1 =>

x*321 = x*256 + x*64 + x

=>

x*321 = x<<8 + x<<5 + x

thats the "lowlevel answer"

It's actually x << 8 + x << 6 + x. Maybe you just hit 5 instead of 6 but yeah, that's the answer I was expecting :)

Mihail121 said:

Isn't shift exactly kind of multiply operator? Oh damn, if you are going to give questions be more specific, ok?

If you are going to answer questions then read the question first. I said dont use the "multiplication operator". I didn't say dont multiply. But either way, shift is not *exactly* a multiply operator. It's more of a subset of the multiply operator - ie: only multiply by powers of two.

Nick said:

Here's an extra question: How to get a pointer to a constructor? Don't take the question too literal, solve the problem.

Nice one! You cant get a direct pointer to the class constructor. apparently msvc is not letting me do what I want again (compiler error C2277) :). But Im guessing that placement new is going to come in handy for this one. a pointer to a function that calls placement new or something. Can you get a pointer to placement new??? That would *technically* be a pointer to the ctor...

Going to try now.

If people got more questions, bring em on :) this is fun.

#14 Kenneth Gorking

    Senior Member

  • Members
  • PipPipPipPip
  • 939 posts

Posted 28 October 2004 - 07:26 PM

Nick said:

bladder said:

Q 011) How do you invoke the ctor and dtor explicitly?
Forget all about Koenig. :D

Here's an extra question: How to get a pointer to a constructor? Don't take the question too literal, solve the problem.

View Post

That's kids stuff. This is for real men!

(Script->*script_t::vftable[*Code++])(Frame);

"Stupid bug! You go squish now!!" - Homer Simpson

#15 Kenneth Gorking

    Senior Member

  • Members
  • PipPipPipPip
  • 939 posts

Posted 28 October 2004 - 07:32 PM

No it's not! What the hell am i doing?!?
"Stupid bug! You go squish now!!" - Homer Simpson

#16 bladder

    DevMaster Staff

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 28 October 2004 - 07:34 PM

Kenneth Gorking said:

That's kids stuff. This is for real men!

(Script->*script_t::vftable[*Code++])(Frame);

View Post


Dude! Some of us are working in the AM without coffee y'know :)

But Id say that that piece of code is calling a bunch of virtual function in the class script_t. Code is an array of the indices of where the function addresses are stored in vftable. And Frame would simply be the standard parameter that all the functions accept.

#17 Kenneth Gorking

    Senior Member

  • Members
  • PipPipPipPip
  • 939 posts

Posted 28 October 2004 - 07:46 PM

It's not virtual functions, but a static array of opcodes supported by my virtual machine, and Code is simply the bytecode dereferenced to give the index to the opcode to execute. Frame is the script execution frame, which holds the call- and variablestack.
The '*script_t::vftable[*Code++]' is just the opcode dereferenced, and the 'Script->' infront of it is the context in which it is executed.
"Stupid bug! You go squish now!!" - Homer Simpson

#18 knackered3

    New Member

  • Members
  • PipPip
  • 19 posts

Posted 28 October 2004 - 07:52 PM

jesus, this is the equivalent to the theme of carving chess pieces in the Shawshank Redemption.
Don't you people have better things to do?
Program some actual stuff...get a job....get a life. Heck, carve some chess pieces.

#19 Ed Mack

    Senior Member

  • Members
  • PipPipPipPip
  • 1239 posts

Posted 28 October 2004 - 08:03 PM

I started self learning C++ this summer, and it's really interesting reading all this :) I tried to get Herbert Schild (sp?)'s C++ ref, as I loved the C version, but my hopeless bookshop didn't stock it (I had gift vouchers for there). It's now on the Amazon Christmas cart :). Is it a good / the best reference book for covering the whole basic language?

#20 bladder

    DevMaster Staff

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 28 October 2004 - 08:17 PM

Nick said:

Here's an extra question: How to get a pointer to a constructor? Don't take the question too literal, solve the problem.

View Post


Got something. Not very fun to execute though...

#include <iostream>
using namespace std;

class Class
{
public:
  int x;

  Class() {
    x = 3;
  }

  void* operator new (size_t sz, void* p)
  {
    return ::new(p) Class();
  }
};

void main()
{
  void* (*ctor)(size_t, void*) = Class::operator new;

  Class* c = (Class*)malloc(sizeof(Class));

  cout<< c->x<< endl;
  
  ctor(sizeof(Class), c);

  cout<< c->x<< endl;

  free(c);
}

[edit]
doh!
Might as well just use a static function instead of overriding the new op.
void* (*ctor)(Class*) = Class::FunctionThatCallsPlacementNew;
ctor( c );
[/edit]





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users