CSE 1322 EXAM 3 MOCK TEST AND STUDY
MATERIAL WITH CODING PROBLEMS AND
DEBUGGING PRACTICE SCENARIOS
◉ The following code will compile and output 8?
class Stuff {public static int x=7;
public static void do_stuff() {x++;System.out.println(x);}}
class Main {
public static void main(String[] args) {
Stuff.do_stuff();
}
}.
Answer: True
◉ class Main {
public static void addtwo(int[] numArray) {
numArray[0]+=2;
}
public static void main(String[] args) {
int[] myNumbers = new int[] {1,2,3,4,5};
,System.out.println(myNumbers[0]);
}
}.
Answer: 1
◉ import java.util.ArrayList;
class Main {public static void main(String[] args)
{ArrayList<Integer> myNumbers = new
ArrayList<Integer>();myNumbers.add(10);myNumbers.add(20);my
Numbers.add(30);myNumbers.add(40);
int num=0;for(int x : myNumbers)
{num+=x;}System.out.println(num);}}.
Answer: 100
◉ Consider the following code for class XYZ
public class XYZ
{
private int myNum;
public XYZ (int n1) {
myNum = n1;
}
}
,Which of the following statements is TRUE about class XYZ.
Answer: Class XYZ inherits from the Object class
◉ In dynamic binding or late binding, the type of object is
determined at run-time..
Answer: True
◉ Given the following code, which of the following is the correct
definition for class B, such that the output of the program is 1 2 3 :
[Hint: Pay close attention to the Access Modifiers]
public class A {
private int x;
private int y;
public A(int a, int b) {
x=a;
y=b;
}
@Override
public String toString() {
return x+" "+y;
, }
}
//Class B goes here.
class Main {
public static void main(String[] args) {
B myB=new B(1,2,3);
System.out.println(myB);
}
}.
Answer: class B extends A {
private int z;
public B(int a, int b, int c) {
super(a,b);
z=c;
}
@Override
public String toString() {
return super.toString()+" "+z;
}
}
MATERIAL WITH CODING PROBLEMS AND
DEBUGGING PRACTICE SCENARIOS
◉ The following code will compile and output 8?
class Stuff {public static int x=7;
public static void do_stuff() {x++;System.out.println(x);}}
class Main {
public static void main(String[] args) {
Stuff.do_stuff();
}
}.
Answer: True
◉ class Main {
public static void addtwo(int[] numArray) {
numArray[0]+=2;
}
public static void main(String[] args) {
int[] myNumbers = new int[] {1,2,3,4,5};
,System.out.println(myNumbers[0]);
}
}.
Answer: 1
◉ import java.util.ArrayList;
class Main {public static void main(String[] args)
{ArrayList<Integer> myNumbers = new
ArrayList<Integer>();myNumbers.add(10);myNumbers.add(20);my
Numbers.add(30);myNumbers.add(40);
int num=0;for(int x : myNumbers)
{num+=x;}System.out.println(num);}}.
Answer: 100
◉ Consider the following code for class XYZ
public class XYZ
{
private int myNum;
public XYZ (int n1) {
myNum = n1;
}
}
,Which of the following statements is TRUE about class XYZ.
Answer: Class XYZ inherits from the Object class
◉ In dynamic binding or late binding, the type of object is
determined at run-time..
Answer: True
◉ Given the following code, which of the following is the correct
definition for class B, such that the output of the program is 1 2 3 :
[Hint: Pay close attention to the Access Modifiers]
public class A {
private int x;
private int y;
public A(int a, int b) {
x=a;
y=b;
}
@Override
public String toString() {
return x+" "+y;
, }
}
//Class B goes here.
class Main {
public static void main(String[] args) {
B myB=new B(1,2,3);
System.out.println(myB);
}
}.
Answer: class B extends A {
private int z;
public B(int a, int b, int c) {
super(a,b);
z=c;
}
@Override
public String toString() {
return super.toString()+" "+z;
}
}