Lecture5

Lecture5 - (1 What does the following program print out class Student cfw int id = 1 String university public static void main(String args cfw int

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
(1) What does the following program print out? class Student { int id = 1; String university; public static void main(String [] args) { int x = 0; Student [] st= new Student[4]; while (x < 2) { st st[x] = new Student(); st[x].id = x; st[x].university = "Ryerson"; x =x +1; } null st[3] =st[1]; st[2] =st[0]; st[0] =st[3]; for (int j=0; j<4; j++) System.out.println ("Student["+j +"]:"+st[j].id); } } id =0 “Ryerson” id=1 “Ryerson”
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 (2) What does the following program print out? class Prob1 { int x, y; boolean test() foo { if (x < y) return true; else return false; } public static void main (String [] args) { Prob1 foo = new Prob1(); foo.x = 3; foo.y = 17; if (foo.test()) System.out.println("yes"); else System.out.println("no"); } } X=3 Y=17
Background image of page 2
3 (3) Given the source code, what is the output? public class CaseTest { public void test(int x) { switch(x) { case 1: System.out.println("One "); case 2: System.out.println("Two "); break; case 3: System.out.println("Three "); default: System.out.println("Do What?"); } } public static void main(String[] args) { CaseTest a = new CaseTest(); a.test(0); a.test(1); } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 (4) public class Swapper { private int x; private String y; public int z; public Swapper(int a, String b, int c){ x = a; y = b; z = c; } public String swap( ) { int temp = x; x = z; z = temp; return y; } public String toString( ) { if (x < z) return y; else return "" + x + z; } } 1) If the statement Swapper s = new Swapper(0, "hello", 0); is executed followed by s.toString( ); what value is returned from s.toString( )? a)
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/12/2010 for the course COE 318 taught by Professor Ken during the Spring '08 term at Ryerson.

Page1 / 11

Lecture5 - (1 What does the following program print out class Student cfw int id = 1 String university public static void main(String args cfw int

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online