Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 - Which of the following will run? 2- If some statement that may crash in fact

ID: 3876707 • Letter: 1

Question

1 -

Which of the following will run?

2-

If some statement that may crash in fact crashes, which of the following will display what the error was that was thrown?

3

  int[] mynumbers = {1,3,5,88};  
  for (int i = 1 ; i <= mynumbers.Length; i++){  mynumbers[i] = 15;  
  }  
  int[] mynumbers = {1,3,5,88};  
  for (int i = mynumbers.Length-1 ; i >= 0; i--){    mynumbers[i] = 15;  
  }  
  int[] mynumbers = {1,3,5,88};  
  ....  
  foreach (int j in mynumbers){  
     j = 15;  
  }  
  int[] mynumbers = {1,3,5,88};  
  for (int i = 1 ; i <= mynumbers.Length; i++){    mynumbers[i] = "hello world";  
  }  

2-

If some statement that may crash in fact crashes, which of the following will display what the error was that was thrown?

  try  
  {  
     // some statement that may crash  
  }  
  catch  
  {  
    MessageBox.Show(ex.Message);  
  }  
  try  
  {  
     // some statement that may crash  
  }  
  finally (Exception ex)  
  {  
    MessageBox.Show(ex.Message);  
  }  
  try  
  {  
     // some statement that may crash  
  }  
  catch (Exception ex)  
  {  
    MessageBox.Show(ex.Message);  
  }  
  if( // some statement that may crash){        catch (Exception ex);    MessageBox.Show(ex.Message);  
  }  

3

Explanation / Answer

1.

Which of the following will run? => Option B will run

int[] mynumbers = {1,3,5,88};

for (int i = 1 ; i <= mynumbers.Length; i++){ mynumbers[i] = 15;

}

== Will not run.. because i varies from 1 to 4, while numbers[4] will give an Array out of bound exception.

int[] mynumbers = {1,3,5,88};

for (int i = mynumbers.Length-1 ; i >= 0; i--){

mynumbers[i] = 15;

}

== Runs fine, as index varies only from 3 to 0.

int[] mynumbers = {1,3,5,88};

....

foreach (int j in mynumbers){

j = 15;

}

== incorrect syntax, foreach loop keyword in for only.

int[] mynumbers = {1,3,5,88};

for (int i = 1 ; i <= mynumbers.Length; i++){

mynumbers[i] = "hello world";

}

== mynumbers is integer, can not accomodate string

===================================================================================

2. Option C is correct

try

{

// some statement that may crash

}

catch

{

MessageBox.Show(ex.Message);

}

== incorrect, as catch doesn't specify any argument

try

{

// some statement that may crash

}

finally (Exception ex)

{

MessageBox.Show(ex.Message);

}

== incorrect, finally can't specify any argument

try

{

// some statement that may crash

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

== Correct, catch block catches any exception and shows it correctly.

if( // some statement that may crash){  

catch (Exception ex);

MessageBox.Show(ex.Message);

}

== incorrect, catch must preceed with a try block.