Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,891 members, 7,802,871 topics. Date: Saturday, 20 April 2024 at 12:20 AM

Programming Tricks: Can You Answer This? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Programming Tricks: Can You Answer This? (1430 Views)

Can You Answer This Question. / LEARN ETHICAL HACKING,TIPS AND TRICKS / Can You Answer This Question. Help Me Learn About It. (2) (3) (4)

(1) (Reply) (Go Down)

Programming Tricks: Can You Answer This? by dontro(m): 7:35pm On Dec 26, 2017
Please see the attached image.
Please:
1. Answer before you try it out,
2. Give technical explanation for your answer

Re: Programming Tricks: Can You Answer This? by Nicolars(m): 5:43am On Dec 27, 2017
The first answer is 1
A do while loop will do at least one loop irrespective of the condition, that is even if the condition isn't met, the code would still run for the first time.
So x is increased by 1 for the first time x=0+1=1;
On the second iteration,the loop is broken because the condition is considered and 1 is not less than 0


The second answer is 9.
The loop is going to iterate 9 times so if 0 increases by 1 nine times , it will be 9
Re: Programming Tricks: Can You Answer This? by philcz(m): 5:52am On Dec 27, 2017
Nicolars:
The first answer is 1
A do while loop will do at least one loop irrespective of the condition, that is even if the condition isn't met, the code would still run for the first time.
So x is increased by 1 for the first time x=0+1=1;
On the second iteration,the loop is broken because the condition is considered and 1 is not less than 0


The second answer is 9.
The loop is going to iterate 9 times so if 0 increases by 1 nine times , it will be 9
The first answer is one as you rightly said but for the second question I think the answer is 10 not 9 because the loop will run from i=0 to i=9 so k=10 before the loop terminates
Re: Programming Tricks: Can You Answer This? by Nicolars(m): 6:12am On Dec 27, 2017
philcz:
The first answer is one as you rightly said but for the second question I think the answer is 10 not 9 because the loop will run from i=0 to i=9 so k=10 before the loop terminates
lol... Yh...

0-9 is 10 counts
Re: Programming Tricks: Can You Answer This? by philcz(m): 6:17am On Dec 27, 2017
Nicolars:
lol... Yh...
0-9 is 10 counts
grin yea. Which languages are good in?
Re: Programming Tricks: Can You Answer This? by Nicolars(m): 8:47am On Dec 27, 2017
philcz:
grin yea. Which languages are good in?

C# and C++

I still be one kind learner shar
Re: Programming Tricks: Can You Answer This? by philcz(m): 9:08am On Dec 27, 2017
Nicolars:


C# and C++

I still be one kind learner shar
Lol.. I see you going far bro.. your logical reasoning is sound
Re: Programming Tricks: Can You Answer This? by Nicolars(m): 9:28am On Dec 27, 2017
philcz:
Lol.. I see you going far bro.. your logical reasoning is sound
Lol... This isn't logic at all grin grin

This question is like breaking toothpick.. And iroko tree still dey forest

1 Like

Re: Programming Tricks: Can You Answer This? by philcz(m): 9:30am On Dec 27, 2017
Nicolars:

Lol... This isn't logic at all grin grin

This question is like breaking toothpick.. And iroko tree still dey forest
Breaking the toothpick will give you the courage to face the iroko. Have high expectations.

2 Likes

Re: Programming Tricks: Can You Answer This? by dontro(m): 3:33am On Dec 29, 2017
Whoops! NONE of you up there got the answer.
Re: Programming Tricks: Can You Answer This? by Darey00(m): 7:32am On Dec 29, 2017
1
and
1
Re: Programming Tricks: Can You Answer This? by stonemasonn: 9:42am On Dec 29, 2017
The first one can't be 1, as long as X<0, console.log might yield 0 or fail.
The second code should yield 9, since index i and variable k both starts with 0 and increments by 1 to i<10.

On a second look, I think K = 0. Because K is outside the for{} loop.
Re: Programming Tricks: Can You Answer This? by NaHiim: 12:11pm On Dec 29, 2017
The first one will display 1 because the code contained within the two braces (do and while) will excecute at least once

The second one will display 0 because its a post increment operator(the increment is done after the variable is read) so it will keep on assigning 0 to k.

1 Like 1 Share

Re: Programming Tricks: Can You Answer This? by Nicolars(m): 1:10pm On Dec 29, 2017
Question 1 would give an error

Because curly braces shouldn't just come after while(k<0)
Re: Programming Tricks: Can You Answer This? by dontro(m): 5:56pm On Dec 29, 2017
Nicolars:
Question 1 would give an error

Because curly braces shouldn't just come after while(k<0)

NOT TRUE. You can have curly braces there, although I don't usually see it used that way.
Re: Programming Tricks: Can You Answer This? by Nicolars(m): 7:04pm On Dec 29, 2017
dontro:


NOT TRUE. You can have curly braces there, although I don't usually see it used that way.

What language is that.... On c++ its not so
Re: Programming Tricks: Can You Answer This? by dontro(m): 6:57am On Dec 30, 2017
Nicolars:


What language is that.... On c++ its not so

That is the most popular programming language in the world today, a.k.a JAVASCRIPT

(1) (Reply)

How To Create Forum Website Like Nairaland / Download AOL Desktop Gold For Windows 10 (april 2020) / This Idea Is Revolutionary

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 20
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.