Test your skills (2005)
Author |
Message |
rizzix
|
Posted: Wed Apr 13, 2005 2:52 pm Post subject: (No subject) |
|
|
the how do u intialise an alias. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Wed Apr 13, 2005 2:55 pm Post subject: (No subject) |
|
|
For clarity, the term is "reference", and if I tell you that it'll give away the answer. Ah, I'll just PM the solution to you. |
|
|
|
|
|
wtd
|
Posted: Wed Apr 13, 2005 4:44 pm Post subject: (No subject) |
|
|
For 30 bits, what's wrong with the following "hello world" program that causes it not to compile?
ada: | with Ada.Text_IO; use Ada.Text_IO;
procedure Hello_World is
begin
Put_Line("Hello world!");
end hello_world; |
|
|
|
|
|
|
wtd
|
Posted: Wed Apr 13, 2005 10:45 pm Post subject: (No subject) |
|
|
For a whopping 100 bits, explain how the following Eiffel classes demonstrate the concept of "uniform access".
code: | class
NAME
creation { ANY }
with_both
feature { ANY }
with_both(f, l : STRING) is
require
f /= "" and l /= ""
do
first := f
last := l
end
full_name : STRING is
do
Result := first + " " + last
end
feature { NONE }
first, last : STRING
end |
code: | class
TEST
creation { ANY }
make
feature { ANY }
make is
local
bob : NAME
do
create bob.with_both("Bob", "Smith)
std_output.put_string(bob.full_name)
std_output.put_new_line
end
end |
|
|
|
|
|
|
wtd
|
Posted: Thu Apr 14, 2005 2:02 am Post subject: (No subject) |
|
|
And, since I thoroughly confused rizzix, a Java-oriented question.
Let's say we have a class for helping us deal with cards.
code: | class Card
{
public static final int HEART = 1;
public static final int DIAMOND = 2;
public static final int CLUB = 3;
public static final int SPADE = 4;
private int suit;
private int card;
public Card(int suit, int card)
{
this.suit = suit;
this.suit = card;
}
public int getSuit()
{
return suit;
}
public int getCard()
{
return card;
}
} |
What's fundamentally wrong with this design? |
|
|
|
|
|
rizzix
|
Posted: Thu Apr 14, 2005 8:04 am Post subject: (No subject) |
|
|
solution? wrote: Type safety and domain restiction is the fundamental problem. The suit of the card is defined as of type 'int' so is the 'card value'.
Problem here, is if you do go ahead with this kind of implementation, it is not type safe hence the client could pass any interger as an argument to the consturctor. Now this it self is the troublesome design problem. But lets assume we want to do that (pass an interger). The issue is that domain is not resticted, hence any interger value may be pass to the constuctor with no runtime error being issued. But this could futher lead to a design problem since, throwing an exception for simply creating a Card object is not a very nice thing to do. Besides as far as possible errors such as these should be caught at Compile time not Runtime.
Solution: use enums. |
|
|
|
|
|
wtd
|
Posted: Thu Apr 14, 2005 1:41 pm Post subject: (No subject) |
|
|
Bingo. |
|
|
|
|
|
wtd
|
Posted: Thu Apr 14, 2005 5:02 pm Post subject: (No subject) |
|
|
Objective-C this time. I expect rizzix to chime in.
Create three classes (Foo, Bar, and Baz), all direct subclasses of Object, such that only Foo and Bar implement a "speak" method which returns void and simply prints the name of the class to standard output. Use stdio.h for the output.
Write a void function "speak" which accepts either a Foo or Bar object, but will produce at least a compile time warning if a Baz object is passed to speak.
The main function should look like:
code: | int main()
{
Foo *foo = [[Foo alloc] init];
Bar *bar = [[Bar alloc] init];
Baz *baz = [[Baz alloc] init];
speak(foo);
speak(bar);
speak(baz);
return 0;
} |
And the implementation of each of the classes should look like:
code: | @implementation Foo
-(void)speak
{
puts("Foo");
}
@end
@implementation Bar
-(void)speak
{
puts("Bar");
}
@end
@implementation Baz
@end |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
rizzix
|
Posted: Fri Apr 15, 2005 2:27 pm Post subject: (No subject) |
|
|
solution wrote: @protocol Speak
- (void) speak
@end
@interface Foo : NSObject <Speak>
{}
@end
@interface Bar : NSObject <Speak>
{}
@end
@interface Baz : NSObject
{}
@end
void speak(id<Speak> k) {
[k speak];
} |
|
|
|
|
|
wtd
|
Posted: Fri Apr 15, 2005 4:06 pm Post subject: (No subject) |
|
|
Nice. |
|
|
|
|
|
[Gandalf]
|
Posted: Fri Apr 15, 2005 4:17 pm Post subject: (No subject) |
|
|
wtd wrote: For 30 bits, what's wrong with the following "hello world" program that causes it not to compile?
ada: | with Ada.Text_IO; use Ada.Text_IO;
procedure Hello_World is
begin
Put_Line("Hello world!");
end hello_world; |
I dont know if this is still valid, but I thought I might as well try...
Is the problem that first letters in the hello_world where you say "end hello_world" not the same case as the procedure "procedure Hello_World"?
btw, I don't know ada |
|
|
|
|
|
wtd
|
Posted: Fri Apr 15, 2005 4:21 pm Post subject: (No subject) |
|
|
[Gandalf] wrote: wtd wrote: For 30 bits, what's wrong with the following "hello world" program that causes it not to compile?
ada: | with Ada.Text_IO; use Ada.Text_IO;
procedure Hello_World is
begin
Put_Line("Hello world!");
end hello_world; |
I dont know if this is still valid, but I thought I might as well try...
Is the problem that first letters in the hello_world where you say "end hello_world" not the same case as the procedure "procedure Hello_World"?
btw, I don't know ada
I suppose now is as good a time as any to tell everyone...
It's a trick question. Ada is a case-insensitive programming language.
hello_world
Hello_World
HELLO_WORLD
hELLO_wORLD
They're all the same.
The program compiles and runs without any problems.
Congratulations to Hikaru79 for figuring this one out. |
|
|
|
|
|
wtd
|
Posted: Sat Apr 23, 2005 11:36 pm Post subject: (No subject) |
|
|
In Haskell:
Will return the number 7 as an int.
Returns 7.0.
Alter the first expression so that it returns 7.0 without using a function, or writing out a float literal, as in the second. |
|
|
|
|
|
rizzix
|
Posted: Sun Apr 24, 2005 12:20 am Post subject: (No subject) |
|
|
solution wrote: 4 + 3 :: Float |
|
|
|
|
|
wtd
|
Posted: Fri May 27, 2005 12:56 pm Post subject: (No subject) |
|
|
I dn't even know if there are enough bits as reward for answering all of these...
That said, prove that you know C++.
Do you know what all of the different uses of "const" or "static" mean?
Do you understand why a normal class can be split into a header and a ".cpp" source file, but a templated class cannot?
Do you understand why you shouldn't use identifiers beginning with two underscores, even though it's technically legal?
Do you understand the difference between:
and
or
and
Do you understand why one of the following works and the other doesn't?
code: | struct foo
{
const int a;
foo(const int init_a)
{
a = init_a;
}
}; |
code: | struct foo
{
const int a;
foo(const int init_a) : a(init_a) { }
}; |
Do you understand why this code is wasteful?
code: | int a[] = {1, 2, 3, 4, 5, 6, 8, 7, 9};
int b[9];
for (int i(0); i < 9; ++i)
{
b[i] = a[i];
} |
Do you know how to replace that loop with a single line of code?
Do you know why one constructor cannot be called from another constructor?
Do you know when and why to define a destructor?
Do you understand how to create and use function objects?
Do you know why the following is bad form?
code: | std::string s[] = {"hello", "world", "foo", "bar"};
std::string concatenated;
for (int i(0); i < 4; ++i)
{
concatenated += s[i];
} |
Do you know what to replace the above with?
Do you know the difference between a struct and a class?
Do you know the difference between the following, and why the first is better code?
code: | struct name
{
const std::string first, last;
name(std::string f, std::string l) : first(f), last(l) { }
std::string full_name() const { return first + " " + last; }
};
struct grade_collection : private std::vector<int>
{
int total() const
{
int sum(0);
for (int i(0); i < size(); ++i)
{
sum += at(i);
}
return sum;
}
int average() const
{
return total() / size();
}
void add_grade(int g)
{
push_back(g);
}
};
struct student : public name, public grade_collection
{
student(std::string f, std::string l) : name(f, l), grade_collection() { }
}; |
code: | struct name
{
const std::string first, last;
name(std::string f, std::string l) : first(f), last(l) { }
std::string full_name() const { return first + " " + last; }
};
struct grade_collection : public std::vector<int>
{
int total() const
{
int sum(0);
for (int i(0); i < size(); ++i)
{
sum += at(i);
}
return sum;
}
int average() const
{
return total() / size();
}
void add_grade(int g)
{
push_back(g);
}
};
struct student : public name, public grade_collection
{
student(std::string f, std::string l) : name(f, l), grade_collection() { }
}; |
Do you understand the difference between the following?
code: | for (int i(0); i < 4; ++i)
{
std::cout << some_vector[i] << std::endl;
} |
code: | for (int i(0); i < 4; ++i)
{
std::cout << some_vector.at(i) << std::endl;
} |
Do you understand why even the second of those is bad, and why the following is better?
code: | for (std::vector<some_type>::iterator i(some_vector.begin()); i != some_vector.end(); i++)
{
std::cout << *i << std::endl;
} |
Demonstrate use a function object combined with std::for_each from the STL to duplicate the above loop, while also allowing for use of any output stream, and not just std::cout. |
|
|
|
|
|
|
|