Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Chuk

Programming test

This topic is 5447 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, can anyone help me with these questions I have to complete for an interview. Many thanks for your help and time. 1. Write a C or C++ function to provide the following functionality ultoa, without using library functions. The function void ultoa( unsigned long l, char* pStr, int base) should convert unsigned long l to an ascii string of base ‘base’ and place in the buffer pointed to by pStr. You may assume that pStr has enough space to hold the string and that base will be in the range 0<=base<=16. 2. Problem code This programmer wonders why “1” is printed all the time, can you tell him? #include <stdio.h> int Func1( void ) { static int n = 0; return n++; } int Func2( void ) { static int n = Func1(); return n+1; } int main() { for( ;; ) { printf( “%d\n”, Func2() ); } } This programmer heard that the max() macro was dangerous so he opted for the relative safety of an inline max() function. So why is 0 printed? #include <stdio.h> typedef unsigned short Word; inline int Max( int n, int m) { return n>m ? n : m; } Word w1=40000,w2 = 0; int main() { printf( “%d\n”, Max( w1, w2) ); return 0; } The following program (split into two files) sometimes prints “3” but prints garbage at other times under different compilers ------------------ file a1.cpp ----------------------------- #include <iostream.h> extern int f(void); extern int b; int a = f(); int main() { cout << b; return 0; } ----------------- file a2.cpp ------------------------------ extern int a; int b=a; int f() { return 3; }

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Chuk
Hello,

can anyone help me with these questions I have to complete for an interview.



So, you want us to help you get a job, thus proving that you are not qualified for the position?

The sheer audacity of this is just...astounding...

-Odd the Hermit

EDIT: After looking at the questions, I'm even more astounded. They're fairly easy, really...

[edited by - Odd the Hermit on August 15, 2003 8:59:27 AM]

Share this post


Link to post
Share on other sites
If that''s for an interview, then they gave you these questions to determine how good YOU are, not me (or anyone else answering). I can still try to help you by giving you some hints.

Question 1:

* You can use long operations : +,-,*,/,%,>>,<< on your long number and your base.

* Using the above operations, you can compute the nth digit of that unsigned long in the selected base (if you don''t know the math, what are you applying for anyway?).

* Once you know all digits, it''s simply a matter of filling the string with the correct digits.

* Don''t forget the ''\0''

Question 2a:

This code doesn''t work because it''s ugly. He should call these functions "Fred" or "Martha". That would be just as explicit as "Func1", but cuter.

More seriously, reading the code line after line, and determining the values taken by the variables is enough to figure out this one. Especially, look at the value of the variable n (you''d better know what static means if you''re applying for this).

Question 2b:

Look up in the first 50 pages on ANY book about learning C or C++, find the section "variables and their size", and you''ll have solved the problem.

Question 2c:

There''s no question asked here, but I assume it would be something like "why?". Try finding out in which order statements are executed and you''ll be done.

I seriously hope you don''t get additional questions the day of your interview, because if you can''t figure out these, you might have a hard time.

[Disclaimer: if you KNOW the solution to these questions, you should have posted it. Because you posted no solution of your own, I reacted like this, because I don''t feel someone who can''t get even the slightest idea about those problems shouldn''t be allowed a programmer job. If you solved these questions alone, then I''m sorry for any agressivity or irony, and I wish you best of luck.]

Share this post


Link to post
Share on other sites
Those are questions for job interview??!?!?!?Cheesh i could solve them on 14 years...

"Tonight we strike,there is thunder in the sky,together we''ll fight,some of us will die,but they''ll always remember that we''ve made a stand and many will die by hand!" - ManOwaR

Share this post


Link to post
Share on other sites
Heh,

These questions are easy, however some of the code is just outright wrong.

For example, question 2. That code shouldn''t even compile. So you would say in the interview, "I would tell the programmer that he''s smoking crack because that code won''t even compile".. or something similar.


lol,

.zfod

Share this post


Link to post
Share on other sites
quote:
Original post by zfod
For example, question 2. That code shouldn''t even compile. So you would say in the interview, "I would tell the programmer that he''s smoking crack because that code won''t even compile".. or something similar.



Of course it should, what make you think it shouldn''t ?


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
quote:
Original post by Mihail121
Those are questions for job interview??!?!?!?Cheesh i could solve them on 14 years...


Really? .. 14 years? . would take me a minute.

Seriously, don''t apply for the job.

Albert

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!