0% found this document useful (0 votes)
6 views24 pages

Practice Test 2

Uploaded by

sarahleblancc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views24 pages

Practice Test 2

Uploaded by

sarahleblancc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 24

COP 3330- Practice Test 2

What does the provided code print to the screen?

//============================================================== Q1:
public class Main {
public static void main(String args[]) {
int [] array = new int [3];
for (int a: array){
a = 100;
a++;
}

for (int a: array){


System.out.print( " "+a);
}
}
}

a ) 0 0 0
b ) 100 101 102
c ) 102 102 102
d ) None of the above

//============================================================== Q2:
public class Main {
public static void main(String args[]) {
int [] array = new int [3];
for (int a =0; i<3; i++){
array[a] = 100;
a++;
}

for (int a: array){


System.out.print( " "+a);
}

}
}

a ) 0 0 0
b ) 100 100 100
c ) 102 102 102
d ) None of the above
//============================================================== Q3:
public class Main {
public static void main(String args[]) {
Test t = new Test();
t.i = 200;
System.out.print(Test.i+" "+ t.i);
}
}

class Test{
private static int i;
public Test(){
i = 100;
}
}
a ) 100 100
b ) 100 200
c ) 200 100
d ) None of the above
//============================================================== Q4:
public class Main {
public static void main(String args[]) {
Test t = new Test();
t.i = 200;
System.out.print(Test.i+" "+ t.i);

}
}

class Test{
public static int i;
public Test(){
i = 100;
}
}

a ) 100 100
b ) 200 200
c ) 200 100
d ) None of the above
//============================================================== Q5:
public class Main {
public static void main(String args[]) {
Test.show();
}

public static show(){


System.out.print("From Main");
}
}

class Test{
public void show(){
System.out.print("From Test");
}
}

a ) From Main
b ) From Test
c ) From TestFrom Main
d ) None of the above
//============================================================== Q6:
public class Main {
public static void main(String args[]) {
Test.show();
}
public static void show(){
System.out.print("From Main");
}
}
class Test{
public static void show(){
System.out.print("From Test");
}
}

a ) From Main
b ) From Test
c ) From TestFrom Main
d ) None of the above
//============================================================== Q7:
public class Main {
public static void main(String args[]) {
System.out.print(Test);
}

class Test{
public String toString (){
return "Test";
}
}

a ) Test
b ) "Test"
c ) None of the above
//============================================================== Q8:
public class Main {
public static void main(String args[]) {
Test t;
System.out.print(t);
}

class Test{
public String toString (){
return "Test";
}
}

a ) Test
b ) t
c ) None of the above
//============================================================== Q9:
public class Main {
public static void main(String args[]) {
Test t = new Test("test");
System.out.print(t);
}

class Test{
private String name;
public Test(String name){
this.name = name;
}

public String toString (){


return "Test";
}
}

a ) Test
b ) test
c ) t
d ) None of the above
//============================================================== Q10:
public class Main {
public static void main(String args[]) {

Test [] array = new Test[3];


System.out.print(array[0]);
}
}

class Test{
private int x;

public Test(){
this.x = -1;
}

public String toString (){


return "x";
}
}

a ) x
b ) 0
c ) -1
d ) None of the above
//============================================================== Q11:
public class Main {
public static void main(String args[]) {

Test [] array = new Test[3];


for (int x = 0; x<3; x++){
array[x] = new Test();
}

System.out.print(array[0]);
}
}

class Test{
private int x;

public Test(){
this.x = -1;
}
public String toString (){
return "x";
}
}
a ) 0
b ) -1
c ) x
d ) None of the above
//============================================================== Q12:

public class Main {


public static void main(String args[]) {

Child c = new Child(1,2);


System.out.print(c);

}
}

class Parent {
private int x;

public Parent(int x){


this.x = x;
}

class Child extends Parent{


private int y;
public Child(int x, int y){
super(x);
this.y = y;
}

public String toString (){


return x+" "+y;
}
}

a ) 1 2
b ) x+" "+y
c ) 1+ +2
d ) None of the above
//============================================================== Q13:
public class Main {
public static void main(String args[]) {

Child c = new Child(1,2);


System.out.print(c);

}
}

class Parent {
protected int x;

public Parent(int x){


this.x = x;
}
}

class Child extends Parent{


private int y;
public Child(int x, int y){
super(x);
this.y = y;
}

public String toString (){


return x+" "+y;
}
}

a ) 1 2
b ) x+" "+y
c ) 1+ +2
d ) None of the above
//============================================================== Q14:

public class Main {


public static void main(String args[]) {

Child c = new Child(1,2);


Parent P = new Parent(3);
System.out.print(P.x);

}
}

class Parent {
protected int x;

public Parent(int x){


this.x = x;
}

class Child extends Parent{


private int y;
public Child(int x, int y){
super(x);
this.y = y;
}

public String toString (){


return x+" "+y;
}
}

a ) 3
b ) 1
c ) 1 2
d ) None of the above
//============================================================== Q15:
public class Main {
public static void main(String args[]) {
Child c = new Child(1,2);
Parent P = new Parent(3);
System.out.print(P.x);

}
}

abstract class Parent {


public int x;

public Parent(int x){


this.x = x;
}

class Child extends Parent{


private int y;
public Child(int x, int y){
super(x);
this.y = y;
}

public String toString (){


return x+" "+y;
}
}

a ) 3
b ) 1
c ) 1 2
d ) None of the above

//============================================================== Q16:
public class Main {
public static void main(String args[]) {

Child c = new Child(1,2);

System.out.print(c.x);

}
}

abstract class Parent {


public int x;

public Parent(int x){


this.x = x;
}

class Child extends Parent{


private int y;
public Child(int x, int y){
super(x);
this.y = y;
}

public String toString (){


return x+" "+y;
}
}

a ) 3
b ) 1
c ) 1 2
d ) None of the above
//============================================================== Q17:
public class Main {
public static void main(String args[]) {

Child c = new Child(1,2);

System.out.print(c.print());

}
}

abstract class Parent {


private int x;

public Parent(int x){


this.x = x;
}

public abstract void print();

class Child extends Parent{


private int y;
public Child(int x, int y){
super(x);
this.y = y;
}

public int print(){


return y;
}
}

a ) 2
b ) 1
c ) y
d ) None of the above
//============================================================== Q18:
public class Main {
public static void main(String args[]) {

Child c = new Child(1,2);

c.print();

}
}
abstract class Parent {
private int x;

public Parent(int x){


this.x = x;
}

public abstract void print();

class Child extends Parent{


private int y;
public Child(int x, int y){
super(x);
this.y = y;
}

public void print(){


System.out.print(y);
}
}

a ) 2
b ) 1
c ) y
d ) None of the above
//============================================================== Q19:
public class Main {
public static void main(String args[]) {
Parent p = new Child (1,2);
p.print();

}
}

abstract class Parent {


private int x;

public Parent(int x){


this.x = x;
}

public abstract void print();

class Child extends Parent{


private int y;
public Child(int x, int y){
super(x);
this.y = y;
}

public void print(){


System.out.print(y);
}
}
a ) 2
b ) 1
c ) y
d ) None of the above
//============================================================== Q20:

public class Main {


public static void main(String args[]) {
Parent p = new Child (1,2);
p.show();

}
}

abstract class Parent {


private int x;

public Parent(int x){


this.x = x;
}

public abstract void print();

class Child extends Parent{


private int y;
public Child(int x, int y){
super(x);
this.y = y;
}

public void print(){


System.out.print(y);
}

public void show (){


System.out.println(y);
}
}

a ) 2
b ) 1
c ) y
d ) None of the above
//============================================================== Q21:

public class Main {


public static void main(String args[]) {
Parent [] p = new Parent[2];
p[0] = new Child1(1,2);
p[1] = new Child2(3,4);

p[0].print();
p[1].print();

}
}

class Parent {
private int x;

public Parent(int x){


this.x = x;
}

public abstract void print();

class Child1 extends Parent{


private int y;
public Child1(int x, int y){
super(x);
this.y = y;
}

public void print(){


System.out.print(y);
}

class Child2 extends Parent{


private int z;
public Child2(int x, int z){
super(z);
this.z = z;
}

public void print(){


System.out.print(z);
}
}

a ) 11
b ) 24
c ) 13
d ) None of the above
//============================================================== Q22:
public class Main {
public static void main(String args[]) {
Parent [] p = new Parent[2];
p[0] = new Child1(1,2);
p[1] = new Child2(3,4);

p[0].print();
p[1].print();

}
}

abstract class Parent {


private int x;
public Parent(int x){
this.x = x;
}

public void print(){


System.out.println(x);
}
}

class Child1 extends Parent{


private int y;
public Child1(int x, int y){
super(x);
this.y = y;
}

public void print(){


System.out.print(y);
}
}

class Child2 extends Parent{


private int z;
public Child2(int x, int z){
super(z);
this.z = z;
}

public void print(){


System.out.print(z);
}
}

a ) 11
b ) 24
c ) 13
d ) None of the above
//============================================================== Q23 :
public class Main {
public static void main(String args[]) {
int x = 1 , y = 0;
try{
if ( y != 0 ) System.out.println( x/y );
}
catch (Exception e) {
System.out.print("1");
}
finally {
System.out.print("2");
}
}
}

a ) 1/0
b ) 2 1
c ) 12
d ) None of the above
//============================================================== Q24 :
public class Main {
public static void main(String args[]) {
String x = "1";
System.out.print(x);
changeTo3(x);
System.out.print(x);
}

public static void changeTo3 (String x) {


x = "3";
}
}

a ) 11
b ) 13
c ) 33
d ) None of the above
//============================================================== Q25 :
class MyString{
public String x;
public MyString(){
x = "1";
}
}
public class Main {
public static void main(String args[]) {
MyString string = new MyString();
System.out.print(string.x);
changeTo3(string.x);
System.out.print(string.x);
}

public static void changeTo3 (String x) {


x = "3";
}
}

a ) 11
b ) 13
c ) 33
d ) None of the above
//============================================================== Q26 :
public class Main {
public static void main(String args[]) {
int x = 1, y=0;
String name = new String("Jon");

try {
System.out.print(name.length());
System.out.print( x/y );

}
catch (NullPointerException e) {
System.out.print("1");
}
catch (ArithmeticException e) {
System.out.print("2");
}
catch (Exception e){
System.out.print("3");
}
finally{
System.out.print("1");
}
}
}

a ) 21
b ) 1231
c ) 321
d ) 123
e ) None of the above
//============================================================== Q27 :
public class Main {
public static void main(String args[]) {
int x = 1, y=0;
String name = null;

try {
System.out.println(name.length());
System.out.println( y/x );
}
catch (NullPointerException e) {
System.out.print("1");
}
catch (ArithmeticException e) {
System.out.print("2");
}
catch (Exception e){
System.out.print("3");
}
finally {
System.out.print("4");
}
}
}

a ) 134
b ) 14
c ) 1234
d ) 104
e ) None of the above
//============================================================== Q28 :
public class Main {
public static void main(String args[]) {
int x = 1, y = 0, z;
z = x/y;

try {
System.out.println( y/x );
}
catch (NullPointerException e) {
System.out.println("1");
}
catch (ArithmeticException e) {
System.out.println("2");
}
catch (Exception e){
System.out.print("3");
}
finally {
System.out.print("4");
}
System.out.print("5");
}
}

a ) 45
b ) 5
c ) 045
d ) None of the above
//============================================================== Q29 :
public class Main {
public static void main(String args[]) {
try{
fun();
System.out.println("1");
}
catch ( Exception e){
System.out.println( "2" );
}
}
public static void fun() throws Exception{
System.out.println(5/0);
}
}

a ) 12
b ) 21
c ) 2
d ) None of the above
//============================================================== Q30 :
public class Main {
public static void main(String args[]) {
try {
fun();
}
catch (Exception e){
System.out.print("1");
}
}

public static void fun() throws NullPointerException{


if ( true )
throw new NullPointerException();
System.out.print("2");
}
}

a ) 2
b ) 1
c ) 12
d ) None of the above
//============================================================== Q31 :
public class Main {
public static void main(String args[]) {
try {
fun();
System.out.print("1");
}
catch (Exception e){
System.out.print("2");
}
System.out.print("3");
}

public static void fun() {


System.out.print(2/0);
System.out.print("1");
}
}

a ) 23
b ) 123
c ) 2
d ) None of the above
//============================================================== Q32 :
class MyClass{
public int data;
public void doNothing() {
}
}

public class Main {


public static void main(String args[]) {
try{
MyClass obj;
System.out.print("1");
}
catch (NullPointerException e){
System.out.print("2");
}

System.out.print("3");
}

a ) 23
b ) 13
c ) 3
d ) None of the above
//============================================================== Q33 :
interface myInterface{
public void doNothing();
}

class myClass{
public static void doNothing() {
System.out.print("chilling!");
}
}

public class Main{


public static void main (String [] someArguments) {
myInterface myinterface;
myClass myobject;
myobject.doNothing();
}
public static void doNothing() {
System.out.print("relaxing");
}
}

a ) relaxing
b ) chilling!
c ) chillingrelaxing
d ) None of the above
//============================================================== Q34 :
abstract class myInterface{
abstract public void doNothing();
}

class MyClass extends myInterface{


public void doNothing() {
System.out.print("chilling");
}
}

public class Main{


public static void main (String [] someArguments) {
(doNothing()).doNothing();
}
public static MyClass doNothing() {
System.out.print("relaxing");
return new MyClass();
}
}

a ) relaxingchilling
b ) relaxing
c ) chillingrelaxing
d ) None of the above
//============================================================== Q35 :
interface MyInterface{
public void doNothing();
}

abstract class MyAbstract implements MyInterface{


abstract public void doNothing(int x);
}
class MyClass extends MyAbstract{
public void doNothing(int x) {
System.out.print("chilling");
}
}

public class Main{


public static void main (String [] someArguments) {
MyClass myobject = new MyClass();
myobject.doNothing();
doNothing();
}
public static void doNothing() {
System.out.print("relaxing");
}
}

a ) relaxing
b ) chilling
c ) chillingrelaxing
d ) None of the above

//============================================================== Q36 :
interface MyInterface{
public void doNothing();
}

abstract class MyAbstract implements MyInterface{


abstract public void doNothing(int x);
public void doNothing() {
System.out.print("chilling");
}
}
class MyClass extends MyAbstract implements MyInterface{
public void doNothing(int x) {
System.out.print("relaxing");
}
}

public class Main{


public static void main (String [] someArguments) {
MyInterface myobject = new MyClass();
myobject.doNothing(4);
}
public static void doNothing() {
System.out.print("relaxing");
}
}

a ) relaxing
b ) chilling
c ) relaxingchilling
d ) chillingrelaxing
e ) None of the above
//============================================================== Q37 :
class MyClass{
public int data;
public String toString() {
return " "+data;
}
}

public class Main {


public static void main(String args[]) {
MyClass [] array = new MyClass [3];

for (MyClass a : array)


a = new MyClass();

try{
for (int i =0; i<3; i++){
array[i].data = i+1;
System.out.print(" "+array[i]);
}
}
catch (Exception e){
System.out.print("BUG");
}
}
}

a ) BUG
b ) 1 2 3
c ) 0 1 2
d ) None of the above
//============================================================== Q38 :
class MyClass{
public int data;
public String toString() {
return " "+data;
}
}

public class Main {


public static void main(String args[]) {
MyClass [] array = new MyClass [3];
MyClass myobject = new MyClass();

try{
for (int i =0; i<3; i++){
array[i] = myobject;
array[i].data = i+1;
}
for (int i =0; i<3; i++)
System.out.print(" "+array[i]);
}
catch (Exception e){
System.out.print("BUG");
}
}
}

a ) BUG
b ) 1 2 3
c ) 0 1 2
d ) 3 3 3
e ) None of the above

//============================================================== Q39 :
class MyClass {
public int data;
MyClass(){
data = 0;
data++;
}
public String toString() {
return " "+data;
}
}

public class Main {


public static void main(String args[]) {

try {
throw new MyClass();
}
catch (MyClass e){
System.out.println(e);
}
}
}

a ) 0
b ) 1
c ) 2
d ) None of the above
//============================================================== Q40 :
class MyClass extends Exception{
public static int data;
MyClass(){
data++;
}
public String toString() {
return " "+data;
}
}

public class Main {


public static void main(String args[]) {
MyClass.data = 1;

try {
throw new MyClass();
}
catch (MyClass e){
MyClass obj = new MyClass();
System.out.println(obj);
}
}
}

a ) 1
b ) 2
c ) 3
d ) None of the above
//============================================================== Q41 :
class ChangeClass{
public static void change(String test) {
test = "Test2";
}
}

public class Main {


public static void main(String args[]) {
String test = "Test1";
ChangeClass.change(test);
System.out.print(test);
}
}

a ) Test2
b ) Test1
c ) test
d ) None of the above
//============================================================== Q42 :
class ChangeClass{
public static String test;
public static void change() {
test = "Test2";
}
}

public class Main {


public static void main(String args[]) {
ChangeClass.test = "Test1";
ChangeClass.change();
System.out.print(ChangeClass.test);
}
}

a ) Test1
b ) Test2
c ) test
d ) None of the above
//============================================================== Q43 :
public class Main {
public static void main(String args[]) {
Test t = null;
try{
t = new Test();
}
catch ( Exception e){
if ( t != null ) System.out.print(t);
}
System.out.print("Test");
}

class Test{
public Test() throws Exception{
throw new Exception();
}
public String toString (){
return "Test";
}
}

a ) Test
b ) TestTest
c ) TestTestTest
d ) None of the above
//============================================================== Q44 :
public class Main {
public static void main(String args[]) {
Iparent p = new Child ();
Child.x = 2;
p.print();

}
}

interface Iparent {
public int x = 1;

public void print();

class Child implements Iparent{


private static int y ;
public Child(){
y = x;
}

public void print(){


System.out.print("Child "+y);
}
}

a ) Child 1
b ) Child 2
c ) Child 1Child 2
d ) None of the above
//============================================================== Q45 :
public class Main {
public static void main(String args[]) {
Iparent[] p = new Iparent[2];

try {
p[0] = new Child(2);
p[0].print();
}
catch (Exception e) {
System.out.println("BAD");
}
}
}

interface Iparent {
public int x = 1;
public void print();
}

class Child implements Iparent {


private int y;

public Child(int y) {
this.y = y;
}

public void print() {


System.out.print(x + " " + y);
}
}
a ) BAD
b ) 2 1
c ) 1 2
d ) None of the above
//============================================================== Q46 :
class Main {

private static int fun(int n) {

if ( n == 0) return 0;
return n + fun(n//2);

}
public static void main(String[] args) {

System.out.println(fun(fun(4)));

}
}

a ) 4
b ) 2
c ) 8
d ) None of the above
//============================================================== Q47 :
class Main {

private static void fun(int age) {


try{
if (age < 0) throw new MyException (age);
}
catch (Exception e){
System.out.print(e);
}
}

public static void main(String[] args) {

fun(65);
System.out.print("done!");
}
}

class MyException extends Exception{


private int age;

public MyException (int age){


this.age = age;
}

public String toString(){


return age + " is invalid" ;
}

a ) -1 is invalid
b ) done!
c ) done-1 is invalid
d ) None of the above

You might also like