Java Eclipse questions

profilefalah5563
preparing_for_the_midterm_2.pdf

John  Foo  with  ID  ;101000123     Code:FJ123   Preparing  for  the  Midterm  2:       Code:  

                   LastInitialFirstInitialLast3numbersID     1. Assume  the  instruction:  

boolean  []  myBoolean  =  new    boolean[10];   a. What  is  the  name  of  the  array  variable?   b. What  type  of  values  the  above  array  is  for?   c. What  is  the  range  of  valid  indexes  values  for  the  above  array?   d. What  will  be  the  value  of  myBoolean.length?  

2.  Using  the  following  segment  code   for(int  i  =  1;  i  <=  5;  i++)  {            System.out.println(i  *  5);   }  

  a)  What  is  the  output  for  this  code?   b)  Augment  the  code  to  output  also  the  squares  of  each  of  the  numbers  1-­‐5.   c)  Modify  the  code  to  print  only  the  when  i  is  even    

3. Write  a  segment  of  code  to  traverse  (go  through  each  element  of)  an  array  of  intergers,   or  size  10,  called  int_array  and  print  out  each  element  to  the  console  

  4. Write  a  segment  code  to  sum  and  print  the  average  of  100  random  numbers  between  1  

and  6.     5. Write  a  segment  of  code  to  traverse  (go  through  each  element  of)  an  array  of  integers,  

or  size  10,  called  int_array  and  print  out  each  element  to  the  console.     6. Explain  the  difference  between  the  control  variables  in  a  while  loop  and  a  for  loop.    

7.    

a. What  is  the  name  of  the  variable  controlling  the  while  loop?   b. In  which  line  is  updated  the  variable  controlling  the  loop?  

c. What  is  the  initial  value  of  the  variable  controlling  the  while  loop?   d. What  is  the  value  of  the  variable  controlling  the  loop  in  line  18?  

8. If  a  variable  controlling  a  while  loop  is  not  updated  inside  of  the  loop  then,  the  loop   goes  for  ever  (infinite  loop)   Example:                    int  control  =  1;                    While  (control  !=    10){                            System.out.println(  “control  “  +  (control  +  2));  

}    

a. This  is  an  infinite  loop;  fix  it   b. Modify  the  above  code  to  print  only  1  3    5    7    9  

9. Write  a  segment  code  for  the  following:(  do  not  worry  for  the  syntax)   Set  a  loop  to  request  a  phrase  from  the  keyboard.  If  the  phrase  contains  the  word  “loop”,   the  boolean  variable  myFlag  is  set  to  false  to  stop  the  loop.     10. From  the  given  loop  answer  the  following  questions:  

a. What  is  the  value  of  the  variable  controlling  the  loop?   b. How  is  the  variable  mutated  or  modified  to  reach  the  value  to  end  the  loop?   c. Is  the  statement  mutating  or  modifying  the  variable  inside  of  the  body  of  the  

loop?   d. When  the  loops  end;  What  is  the  value  of  the  variable  controlling  the  loop:  

11. Define  the  data  structure  array.  Include  uses;  what  represent  the  name  of  the  array;   importance  of  the  index  value;  naming  of  the  variables  bundle  within  the  array.  

12. Describe  how  do  you  declare  an  array   13. How  the  arrays  in  java  are  initialized?   14. Describe  how  do  you  print  the  content  of  an  array   15. Describe  how  do  you  traverse  an  array   16. Explain  the  code  below:  what  is  the  input?;output?  Transformed  data?  

17. What  is  the  problem  in  this  segment  code?          

           

     

  18. Write  a  segment  code  to  request  the  user  a  number  from  1  to  15;  then  print  a  pyramid  

to  that  number.  Example  if  the  user  input  8,  the  output  is  the  following   1   12   123   1234   12345   123456   1234567   12345678    

19.  Using  the  following  code  answer  the  questions:   for  (int  i  =  0;  i  <  n;  i++)          {  a[i]  =  Math.random();  }   a. What  is  the  name  of  the  array?   b. What  is  the  variable  indexing  the  array?   c. What  is  the  range  of  the  indexing  variable?  

20. Using  the  following  code  answer  the  questions:   for  (int  i  =  0;  i  <  n;  i++)                {  System.out.println(“a  “+    i    +  ”,  “  +    a[i]);  }    

a. What  ‘the  statement  in  the  loop’  is  printing?