PDA

View Full Version : Test your skill in PL and ask real questions



king_143
10-24-2003, 11:43 PM
Lets post interesting PL questions and solve them. If you have any programing questions kindly post them we all can try solving them

Here's one

This question was asked in aptitude test of Intel.

int i, n = 20;
for (i = 0; i < n; i--)
printf("-");

You have to print '-' 20 times. by adding or removing or
replacing only a single character from the code.

cheers,
Arif


Wish you all happy diwali

silican
10-25-2003, 01:31 AM
How about adding a negative sign for the variable n in the initialisation step ??

n = -20

arumugam57
10-25-2003, 10:09 PM
Silican!!
(i j both are integers. then n = -20? :lol::lol::lol::lol::lol:)

i=0; n=-20 ; so first loop i< n = false. So you will not get even a sinle time output dear.

I am thinking and trying all possible solutions bt not geeting.

let me try for some more time.

dinesh
10-26-2003, 12:18 AM
replace
for (i = 0; i < n; i--)
by
for (i = 0; -i < n; i--)

arumugam57
10-26-2003, 12:27 AM
Shidinesh!! Nee periya puliyaa nee!!
I tried putting - everywhere except that place what u mentioned.

Nethu fulla thala vali intha program pathi think panni panni..

Ada chey..

You are so smart..

Cheers.

dinesh
10-26-2003, 12:37 AM
aahaa....over-aa ice vekkatha raajaa.......etho nammaala mudinjathu...

dinesh
10-26-2003, 12:42 AM
ok.....another question.....
here an 'object' means an object as in OOP

If A and B are two objects, how would you test if both the objects are identical (I'm talking about identical here, not about equal), if the 'isEqual' (or '==') test is disabled?

as an afterthought.....this might be quite easy....hmmm.... :think:

king_143
10-26-2003, 10:42 PM
Here is the solution for the question

one by shidinesh
for( i=0; -i<n ; i--);
and other one
for(i=0; i<n;n--);

Thank you for the response

Bye
n k

king_143
10-26-2003, 10:43 PM
Sorry
It the other one is
for(i = 0 ; i < n ; n--);

Bye,
n k

king_143
10-26-2003, 11:19 PM
Shidinesh,
here's a try,

1. See if A.toString() and B.toString() gives the same result,

2. otherwise the C function.

First find the size of A and B if not equal then say not identical else

int i = memcmp( (char *)&A, (char *)&B, sizeof(A));

if i = 0 the identical else not.

I' SORRY I DO NOT HAVE A COMPLIER TO VERIFY MY IDEAS BEFORE POSTING IT HERE.

bye,
n k s

dinesh
10-26-2003, 11:28 PM
sorry king_143....this is not the correct answer.......
both your tests will only check whether the objects are equal, whereas the question is for identical objects.....
ie,
String A = new String("Hello");
String B = new String("Hello");
means A and B are equal.

But,
String A = new String("Hello");
String B = A;
means A and B are identical.....

Also, I'm sorry I don't know much C, so I can't understand what your code here is doing. Can you please explain it to me?

king_143
10-26-2003, 11:43 PM
Here's a puzzle asked in a WIPRO INTERVIEW

(PLEASE TELL ME IF YOU FEEL THIS HAS GONE OUT OF SCOPE, I found it interesting so i am posting)

There is a building with two floors.And there is a bulb in the top floor of the building and there are three switches in the lower floor. How will you identify which is the right switch for the bulb. Assuming the bulb is intially in off. you are on the lower floor and allowed to go the top floor only once.

Please dont make absurd assumptions like you go with a friend, have a telescope etc....

There is a valid solution.

Bye
n k su

king_143
10-26-2003, 11:50 PM
Since A and B are objects in Java. The have the parent calls Object methods in it.
So,
A.equals(B) or B.equals(A) should work.

If Im not suppossed to use this method tell me I will try again.

Bye,
n k sur

king_143
10-26-2003, 11:56 PM
Friends ,
Can you PLEASE MENTION THE LANGUAGE THE QUESTION YOU POST BELONGS TO. (Even I will do it from now) Because too many languages out there , it becomes diffuclt to guess. If you think it is a general PL question or puzzle etc. please mention it before hand.

Thank you,

Bye,
n k sure

dinesh
10-26-2003, 11:59 PM
So,
A.equals(B) or B.equals(A) should work.

If Im not suppossed to use this method tell me I will try again.

Yes...you are not supposed to use this method. If you can use it then the question would be really easy.... :)

king_143
10-27-2003, 12:50 AM
If I know the members varaibles then I can test if a change in one reflects in the other.

If not satisified tell me I will give a try again.

dinesh
10-27-2003, 08:43 PM
If I know the members varaibles then I can test if a change in one reflects in the other.

yes...that's the general idea....

king_143
10-27-2003, 10:29 PM
Final try,

class X A,B;
A = new X();
B = A;

Now,
Class X C;

C = A;
if( A != null and B!=null)
{
C = A;
A = null;
if ( B == null )
{
output(" A and B are indentical");
}
else
{
output(" output A and B are not identical");
}
A = C;
}

Last try , wating for the answer.

Bye,
n k

dinesh
10-28-2003, 12:02 PM
your previous answer was correct. You have to make changes in a member variable of an object and check the other object. If that also had the same changes, then they are identical....

in your last example you are assuming B=A, and hence they will be identical whatever you do......

king_143
10-28-2003, 06:43 PM
Friends,
Any more questions , kindly post them.

Bye,
n k

king_143
10-28-2003, 06:45 PM
// PL - ANSI C
int i = 1000, j = 1000;
long int k;
k = i * j;

What is the problem in the above statements and how to rectify it?

king_143
10-28-2003, 06:47 PM
Friends,
Any try for the puzzle I posted earlier.
Bye,
n k

vennai1
10-28-2003, 09:08 PM
ennaiyaa...ellaarum Programming-la kalakureenga... :think:

I know little bit fortran...but athu kuuda maranthu pochu ippo... :( :(


dei king summa oru guess da...

3 and 4 lines must be made into one statement ?? :think:

or 1000 * 1000 is a big number... you cannot store ?? :think:


thappunna sorry da... programming pathi enakku neraiya theriyaathu :(

sathy
10-28-2003, 09:56 PM
// PL - ANSI C
int i = 1000, j = 1000;
long int k;
k = i * j;

What is the problem in the above statements and how to rectify it?

i don't see any problem in 'C' as such. not sure if ANSI C requires type casting. according to me, the above chunk of code is correct assuming int as 4 bytes.

rnu_143
11-08-2003, 01:29 AM
The solution for the puzzle I posted earlier is

I MAKE THE FOLLOWING ASSUMPTIONS.
1. Intially the bulb is off
2. The bulb get heated after a certain amount of time
x
3. The bulb remains hot for the time y
4. Time taken to reach top floor is z
5. and z < y.

I number the switches 1,2,3 with out loss of
generality

switch on 1 for time x
switch 1 off and switch on 2
go up stairs, if bulb is glowing then it is switch 2
if bulb cold and not glowing then it is switch 3
else it is switch 1

rnu_143
11-08-2003, 01:34 AM
The problem in the code lies
i and j are both integer of 2 bytes of length.
C multiples both as integer quantites and it uses 2 bytes to store the result
This stored result is then transferred to k, hence k does not have the actual value 1000*1000, even though it of 4 bytes in length.

rectification

k = (long)i*j;

king_143
11-08-2003, 01:37 AM
I think you got the answer from the other me

Bye

dinesh
11-08-2003, 01:37 AM
yeah....seems right......I never am very confident with C data types. :(

king_143
11-08-2003, 01:39 AM
I think this looks better than the one before

1. Switch on 1 for 5 min.
2. Switch off 1 and switch on 2.
3. Goto top floor
4. If bulb is glowing, answer is 2.
5. If bulb is not glowing, touch and see it - if hot (or warm),
answer is 1 else, answer is 3.

dinesh
11-08-2003, 01:40 AM
guys, is king_143 == rnu_143 ? :)

king_143
11-08-2003, 02:46 AM
Questions pleaseeeee.....

Kindly ask some question related to PL to keep this topic running and also to gain and share some ideas

sri_gan
11-08-2003, 03:02 AM
What is PL?

dinesh
11-08-2003, 12:54 PM
programming language

king_143
11-09-2003, 02:39 AM
What could be wrong in the following statements. And why?
"THIS IS NOT A EASY ONE, atleast for me"


char *s1 = "Hello, ";
char *s2 = "world!";
char *s3 = strcat(s1, s2);

dinesh
11-09-2003, 02:50 AM
I don't know much C, but here goes.....

the second argument of function strcat should be a constant string, and s2 is not constant....

sri_gan
11-09-2003, 04:13 AM
What could be wrong in the following statements. And why?
"THIS IS NOT A EASY ONE, atleast for me"


char *s1 = "Hello, ";
char *s2 = "world!";
char *s3 = strcat(s1, s2);



C doesn't support a variable assigment in the place of a constant, which is declaration.

It should go like

char *s3;

Then use strcpy function to copy the concatenated values(s1,s2) into the variable s3.

Hope this helps.

king_143
11-11-2003, 02:07 AM
EVEN I COULD NOT SOLVE THIS PROBLEM ( THIS QUESTION IS NOT FOR US , I THINK)

THE ANSWER.

The first problem is with the first two statements
The string literal is used in two ways
1. As an array intailizer , like char a[] = "string"
2. it is convereted into a static string, which may be stored in a read-only memeory.

Hence by chance if the first strings i.e, s1 has its "hello" stored in a read-only memory then the strcat(s1,s2) [ concats s2 to s1 ] could crash your program.

If we escape this the third statement strcat(s1,s2) does not intialize an array of characters for s3, it would just return s1 which would go into s3.

As s1 does not sufficient size to hold it s3 may give strange results.

Solution would be changing the first statement to
char s1[20] = "Hello, ";

king_143
11-11-2003, 02:09 AM
IF you have any questions please post it. I am sure it will help to improve our skills.

mpalanieppan
11-12-2003, 06:25 AM
Does anybody used/know functional programming languages?SML/Lisp/Prolog....Has anybody done any significant project on these languages?........It is generally used in teaching programming classes, but I have never come across any implementations...?...

mpalanieppan
11-12-2003, 06:51 AM
Sorry about the other posts...i did not know about the software forum (but still i hope someone can answer...) But anyway a relevant question: I want to learn C#, which is almost same as Java...am involved in writing code for testing serial communications between a plane and ground station etc, etc....I am looking for a good book that is at the professional level to help me learn all the C# programming techniques, especially harder stuff like threading etc...there are so many series of books out there. Does anybody know/have used any good book/any suggestions? I know there are online forums but still i figure a book would be quite useful...appreciate ur suggestions...

M.Palanieppan

dinesh
11-12-2003, 08:54 AM
Does anybody used/know functional programming languages?SML/Lisp/Prolog....Has anybody done any significant project on these languages?........It is generally used in teaching programming classes, but I have never come across any implementations...?...

I have done some work in functional languages. Mainly in Haskell. But, also some ML. Prolog is more of a logical language. What do you want to know in functional programming?

For your Linux queries, there is a forum called 'Linux Help' under 'Technical help - Hardware, software related'. I will move your queries there.

As for C#, it is very similar to Java. So if you know Java, it's just a question of learning the keywords. Microsoft Visual Studio software is an exceelent resource and has extensive documentation of C#, and also helps you program. If you are looking for a book, check out www.wrox.com who have some good books about programming

king_143
11-12-2003, 11:20 PM
mpalanieppan
If you are familiar with C, C++. Then look for functional programming C++ or FC++. That shoulbe be a good way to start insted of learning a whole new language.

king_143
11-12-2003, 11:22 PM
Here a brief about FC++

FC++ is a library for functional programming in C++. Functional programming is a programming paradigm in which functions are treated as regular values. Thus, we can have functions that take other functions as parameters.

king_143
11-12-2003, 11:35 PM
A resoruce for FC++ from her developers Brian McNamara and Yannis Smaragdakis

http://www.cc.gatech.edu/~yannis/fc++/download.html

You have examples there and can try working out them

king_143
11-12-2003, 11:39 PM
A tip for those who are programmers

Never do this

*********************
double a, b;
...
if(a == b) /* WRONG */

*******************


use something like

#include <math.h>
...
if(fabs(a - b) <= epsilon * fabs(a))
for some suitably-chosen epsilon.

king_143
11-17-2003, 01:43 AM
Are the following switch statements valid?

I.

int i;
/* .. */

switch (i) {
case 1:
/* .. */
break;
case 1 * 2 + 4:
/* .. */
break;
}

II.

int i;
/* .. */

switch (i) {
}

king_143
11-17-2003, 10:21 PM
Friends,
I am waiting for your reply. The above questions are not tough and are pretty straight forward. Kindly try it , you can guess or mention your attempts.
It is difficult to for me to post questions without any attempts.

Thank you,

n k

sathy
11-17-2003, 10:43 PM
Are the following switch statements valid?

I.

int i;
/* .. */

switch (i) {
case 1:
/* .. */
break;
case 1 * 2 + 4:
/* .. */
break;
}

Yes. It is valid in MS C/C++ compiler. Not sure with ANSI.



II.

int i;
/* .. */

switch (i) {
}
Blank switch block is also allowed in the same.

King! people won't try to answer the questions if its very simple as above. Don't just stick with the syntax. Post something better n keep up with your good work :D

sgrajan
11-26-2003, 08:21 PM
King, enakku wipro questionnukku answer theriyale,
Plz sollungalen.
Bye
SG

sundaraveena
11-26-2003, 09:04 PM
that bulb-switch problem is a classic, there are variations in the problem like how to determine in case of 3 bulbs and three switches also

king_143
11-26-2003, 09:04 PM
I think this explanation should help,
1. Switch on 1 for 5 min.
2. Switch off 1 and switch on 2.
3. Goto top floor
4. If bulb is glowing, answer is 2.
5. If bulb is not glowing, touch and see it - if hot (or warm),
answer is 1 else, answer is 3.

mpalanieppan
11-30-2003, 07:26 AM
What are the considerations and techniques in choosing optimal hash functions for hash tables? No specific problem. Just curious.

king_143
12-04-2003, 07:00 PM
I am sorry I have almost forgot about hash tables, But DBMS by korth has a topic on how they use hash tables and functions for DBMS storage and retrival I think it would be a good resource. You can also refer tenenbaum Data Struc in C and C++

Sorry for the delayed reply