Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Test your C++ knowledge with these questions!

Test your C++ knowledge with these questions!

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++databasedata-structures
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    soer
    wrote on last edited by
    #1

    Hi, I just came up with some quick C++ problems that I thought some people might like to try out. Post your answers as replies. Good luck! 1) Identify any errors in the following declarations. a. int x[4] = { 8,7,6,4,3 }; b. int x[] = { 8,7,6,4 }; c. const int SIZE = 4; int x[SIZE]; 2) What is wrong with the following piece of code? int sample_array[10]; for (int index=1; index<=10; index++) sample_array[index] = 3*index; 3) Suppose we expect the elements of the array a to be ordered so that a[0] <= a[1] <= a[2] <=… However, to be safe we want our program to test the array and issue a warning in case it turns out that some elements are out of order. The following code is supposed to output such a warning, but it contains a bug. What is it? double a[10]; for (int index = 0; index <10; index++) if(a[index] > a[index +1]) cout <<”Array elements”<

    N F 2 Replies Last reply
    0
    • S soer

      Hi, I just came up with some quick C++ problems that I thought some people might like to try out. Post your answers as replies. Good luck! 1) Identify any errors in the following declarations. a. int x[4] = { 8,7,6,4,3 }; b. int x[] = { 8,7,6,4 }; c. const int SIZE = 4; int x[SIZE]; 2) What is wrong with the following piece of code? int sample_array[10]; for (int index=1; index<=10; index++) sample_array[index] = 3*index; 3) Suppose we expect the elements of the array a to be ordered so that a[0] <= a[1] <= a[2] <=… However, to be safe we want our program to test the array and issue a warning in case it turns out that some elements are out of order. The following code is supposed to output such a warning, but it contains a bug. What is it? double a[10]; for (int index = 0; index <10; index++) if(a[index] > a[index +1]) cout <<”Array elements”<

      N Offline
      N Offline
      Navin
      wrote on last edited by
      #2

      soer wrote: 1) Identify any errors in the following declarations. a. int x[4] = { 8,7,6,4,3 }; b. int x[] = { 8,7,6,4 }; c. const int SIZE = 4; int x[SIZE]; Yes, the array x is being defined 3 different times. soer wrote: 2) What is wrong with the following piece of code? int sample_array[10]; You should be using std::vector instead of a static array. soer wrote: 3) Suppose we expect the elements of the array a to be ordered so that a[0] <= a[1] <= a[2] <=… for (int index = 0; index <10; index++) if(a[index] > a[index +1]) cout <<”Array elements”< Yes, there is no space between the words and the numbers being printed out. soer wrote: 4) Consider the following function definition: Void tripler(int& n) { n=3*n; } That should be: n *= 3; and "Void" should have a lower-case v. soer wrote: 5) What (if anything) is wrong with the following code? The definition of tripler is given in #11. int b[5]={1,2,3,4,5}; for (int i=1;i<=5;i++) tripler(b[i]); The line containing "tripler" is not indented properly. soer wrote: 6) Which of the following are acceptable function calls? too2(my_array, 29); too2(my_array, 10); The function name is incorrect. It should be "tutu". So how did I do? :-D :-D Sometimes I feel like I'm a USB printer in a parallel universe.

      A S 2 Replies Last reply
      0
      • N Navin

        soer wrote: 1) Identify any errors in the following declarations. a. int x[4] = { 8,7,6,4,3 }; b. int x[] = { 8,7,6,4 }; c. const int SIZE = 4; int x[SIZE]; Yes, the array x is being defined 3 different times. soer wrote: 2) What is wrong with the following piece of code? int sample_array[10]; You should be using std::vector instead of a static array. soer wrote: 3) Suppose we expect the elements of the array a to be ordered so that a[0] <= a[1] <= a[2] <=… for (int index = 0; index <10; index++) if(a[index] > a[index +1]) cout <<”Array elements”< Yes, there is no space between the words and the numbers being printed out. soer wrote: 4) Consider the following function definition: Void tripler(int& n) { n=3*n; } That should be: n *= 3; and "Void" should have a lower-case v. soer wrote: 5) What (if anything) is wrong with the following code? The definition of tripler is given in #11. int b[5]={1,2,3,4,5}; for (int i=1;i<=5;i++) tripler(b[i]); The line containing "tripler" is not indented properly. soer wrote: 6) Which of the following are acceptable function calls? too2(my_array, 29); too2(my_array, 10); The function name is incorrect. It should be "tutu". So how did I do? :-D :-D Sometimes I feel like I'm a USB printer in a parallel universe.

        A Offline
        A Offline
        Antony M Kancidrowski
        wrote on last edited by
        #3

        hehehe :-D Ant.

        R 1 Reply Last reply
        0
        • A Antony M Kancidrowski

          hehehe :-D Ant.

          R Offline
          R Offline
          ravjak
          wrote on last edited by
          #4

          Too easy :). Pain is a weakness living the body

          1 Reply Last reply
          0
          • S soer

            Hi, I just came up with some quick C++ problems that I thought some people might like to try out. Post your answers as replies. Good luck! 1) Identify any errors in the following declarations. a. int x[4] = { 8,7,6,4,3 }; b. int x[] = { 8,7,6,4 }; c. const int SIZE = 4; int x[SIZE]; 2) What is wrong with the following piece of code? int sample_array[10]; for (int index=1; index<=10; index++) sample_array[index] = 3*index; 3) Suppose we expect the elements of the array a to be ordered so that a[0] <= a[1] <= a[2] <=… However, to be safe we want our program to test the array and issue a warning in case it turns out that some elements are out of order. The following code is supposed to output such a warning, but it contains a bug. What is it? double a[10]; for (int index = 0; index <10; index++) if(a[index] > a[index +1]) cout <<”Array elements”<

            F Offline
            F Offline
            FlyingDancer
            wrote on last edited by
            #5

            Maybe those problems are concerned with as folloing: 1.Overflow 2.Reference ... and I can't understand the last problem. :)

            1 Reply Last reply
            0
            • N Navin

              soer wrote: 1) Identify any errors in the following declarations. a. int x[4] = { 8,7,6,4,3 }; b. int x[] = { 8,7,6,4 }; c. const int SIZE = 4; int x[SIZE]; Yes, the array x is being defined 3 different times. soer wrote: 2) What is wrong with the following piece of code? int sample_array[10]; You should be using std::vector instead of a static array. soer wrote: 3) Suppose we expect the elements of the array a to be ordered so that a[0] <= a[1] <= a[2] <=… for (int index = 0; index <10; index++) if(a[index] > a[index +1]) cout <<”Array elements”< Yes, there is no space between the words and the numbers being printed out. soer wrote: 4) Consider the following function definition: Void tripler(int& n) { n=3*n; } That should be: n *= 3; and "Void" should have a lower-case v. soer wrote: 5) What (if anything) is wrong with the following code? The definition of tripler is given in #11. int b[5]={1,2,3,4,5}; for (int i=1;i<=5;i++) tripler(b[i]); The line containing "tripler" is not indented properly. soer wrote: 6) Which of the following are acceptable function calls? too2(my_array, 29); too2(my_array, 10); The function name is incorrect. It should be "tutu". So how did I do? :-D :-D Sometimes I feel like I'm a USB printer in a parallel universe.

              S Offline
              S Offline
              soer
              wrote on last edited by
              #6

              Syntaxx and code form is not a problem in any of the problems here. Spaces b/w words dont matter nor does indentation. Try again!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups