Preview

06 - AP Exam question set #6 (Hard)

 1. Evaluate the following Java expression, if x=3, y=5, and z=10:
Expression: ++z + y - y + z + x++

  23

  20

  25

  24

 2. Whenever the first number is smaller than the second, the remainder is the second number.
System.out.println(2 % 3);

  TRUE

  FALSE

 3. The following code prints 135.
System.out.println("13" + 2 + 3);

  FALSE

  TRUE

 4. Assume that SomeClass and MainClass are properly defined in separate files. What is the output of main()?
class SomeClass
{
    int someVar;
}

public class MainClass
{
    public static void main(String[] args)
    {
        SomeClass x;
        System.out.println(x.someVar);
    }
}

  Answer: 0

  runtime error

  unknown value

  compile error

 5. The above code gives error that ____________________________ with a call to the SomeClass constructor.

  x has not been instantiated. It needs to be instantiated

  x has not been created as an object. It needs to be created

  x has not been initialized. It needs to be initialized

  x has not been correctly defined as a type. It needs to be defined

 6. Refer to the following code. If there were curly braces around the two indented statements, then:
int x = 3;
int y = 2;
if (y / x > 0)
   System.out.print("test ");
   System.out.print("track ");

  test track will be printed

  nothing will be printed

  track will be printed

  test will be printed

 7. The following code:
int findSmallest(int[] values) {
  int minIndex = 0;
  for (int i=1; i<values.length; i++) {
    if (values[i] < values[minIndex]) {
      minIndex = i;
    }
  }
  return minIndex;
}

  iterates over a non-empty array to find the index of the smallest element

  iterates over an empty array to find the index of the largest element

  iterates over a non-empty array to find the element contained in the first index

  iterates over a non-empty array to find the index of the first indexed element

 8. Given this code, which of the following will cause an infinite loop? Assume temp is an int variable initialized to be greater than zero and that a is an array of ints.
for ( int k = 0; k < a.length; k++ )
{
   while ( a[ k ] < temp )
   {
      a[ k ] *= 2;
   }
}

  When all values in a are larger than temp.

  Whenever a has values larger then temp.

  The values don't matter this will always cause an infinite loop.

  Whenever a includes a value that is less than or equal to zero.

 9. If nums has been created as an ArrayList object and initially contains integer values: [0, 0, 4, 2, 5, 0, 3, 0], what will nums contain as a result of executing method numQuest?
private List nums;

//precondition: nums.size() > 0
//nums contains Integer objects
public void numQuest() {
   int k = 0;
   Integer zero = new Integer(0);
   while (k < nums.size()) {
      if (nums.get(k).equals(zero))
         nums.remove(k);
      k++;
   }
}

  [4, 2, 5, 3]

   [0, 4, 2, 5, 3]

  [0, 0, 4, 2, 5, 0, 3, 0]

   [3, 5, 2, 4, 0, 0, 0, 0]

 10. What is the output of the following program?
public class testincr
{
public static void main(String args[])
{
int i = 0;
i = i++ + i;
System.out.println(?I = ? +i);
}
}

  I = 1

  I = 2

  I = 0

  I = 3

 11. What needs to go on line 7 in order for the code to print: 101 Adam
class Student{  
 int id;  
 String name;  
}  
class TestStudent3{  
 public static void main(String args[]){  
  _____________________________ 
  //Initializing objects  
  s1.id=101;  
  s1.name="Adam";  
  //Printing data  
  System.out.println(s1.id+" "+s1.name);   
 }  
}  

  s1 = new Student(student);

  s1 = Student new student();

  Student s = new student();

  Student s1=new Student();

 12. What is the output of the following program?
class recursion {
        int func (int n) {
            int result;
            if (n == 1)
                return 1;
            result = func (n - 1);
            return result;
        }
    } 
    class Output {
        public static void main(String args[]) {
            recursion obj = new recursion() ;
            System.out.print(obj.func(5));
        }
    }

  1

  120

  2

  None of the listed answers are correct

 13. The output of the following program is 120.
class recursion {
        int fact(int n) {
            int result;
            if (n == 1)
                return 1;
            result = fact(n - 1) * n;
            return result;
        }
    } 
    class Output {
        public static void main(String args[]) {
            recursion obj = new recursion() ;
            System.out.print(obj.fact(5));
        }
    }

  False

  True

 14. What is the output of the following program?
public class MyFirst {  
      public static void main(String[] args) {  
         MyFirst obj = new MyFirst(n);  
 }  
 static int a = 10;  
 static int n;  
 int b = 5;  
 int c;  
 public MyFirst(int m) {  
       System.out.println(a + ", " + b + ", " + c + ", " + n + ", " + m);  
   }  
// Instance Block  
  {  
     b = 30;  
     n = 20;  
  }   
// Static Block  
  static   
{  
          a = 60;  
     }   
 }  

  10, 5, 0, 20, 0

  60, 5, 0, 20

  60, 30, 0, 20, 0

  10, 30, 20

 15. ABH8097 would be considered a valid long literal.

  TRUE

  FALSE