CSE 1322 LAB AND LECTURE EXAM 3
COMPLETE PREPARATION GUIDE WITH DATA
STRUCTURES AND ALGORITHMS BASICS
◉ Java
class Numbers {
public int a;
public static int b;
public Numbers(int c) {
a+=c; b+=c; }
public int get_b() {
return b; }}
class Main {
public static void main(String[] args) {
Numbers n1=new Numbers(2);
Numbers n2=new Numbers(3);
System.out.println("a: "+n2.a+" b: "+Numbers.b);}}.
Answer: a: 3 b: 5
◉ int x=10;for(int i=5;i<0;i--) {x++;}System.out.println(x);.
Answer: 10
,◉ What is the output of the following statements:
//Java:
String s="Hi";
for(int i=0;i<3;i++) {
System.out.print("S"+s);
}.
Answer: SHiSHiSHi
◉ Which of the following are valid variable declarations:
(Select ALL that are valid).
Answer: int a=1102;
long b=1;
Java:
String d="";
C#:
string d="";
◉ Which of the following blocks of code will successfully ask a user
for a number, multiply that number by 2, and print the result.
Answer: Java
import java.util.Scanner;
,System.out.println("Enter a number to be doubled:");
Scanner myscanner = new Scanner(System.in);
int num=myscanner.nextInt();
num*=2;
System.out.println("Your number doubled is "+num);
◉ Java
boolean a=false;
boolean b=true;boolean c=false;boolean d=true;
if((a) && (b || c) && d) {
System.out.println("Yes");
}else {
System.out.println("No");}.
Answer: No
◉ The following code will not compile as is. It is missing a line that
needs to be inserted where the "//Missing Line" comment is. Which
line of code will fix the compile error?
class House {
public int square_footage;
public int stories;
, public House() {
square_footage=0;stories=1;}}
class Main {public static void main(String[] args) {
House h1;
//Missing Line
h1.square_footage=1;}}.
Answer: h1 = new House();
◉ class Stuff {
public int number=1;
}
class Main {
public static void do_things(int y) {
y+=3;
}
public static void do_other_things(Stuff y) {
y.number+=3;
}
public static void main(String[] args) {
int a=3;
Stuff myStuff = new Stuff();
do_things(a);
COMPLETE PREPARATION GUIDE WITH DATA
STRUCTURES AND ALGORITHMS BASICS
◉ Java
class Numbers {
public int a;
public static int b;
public Numbers(int c) {
a+=c; b+=c; }
public int get_b() {
return b; }}
class Main {
public static void main(String[] args) {
Numbers n1=new Numbers(2);
Numbers n2=new Numbers(3);
System.out.println("a: "+n2.a+" b: "+Numbers.b);}}.
Answer: a: 3 b: 5
◉ int x=10;for(int i=5;i<0;i--) {x++;}System.out.println(x);.
Answer: 10
,◉ What is the output of the following statements:
//Java:
String s="Hi";
for(int i=0;i<3;i++) {
System.out.print("S"+s);
}.
Answer: SHiSHiSHi
◉ Which of the following are valid variable declarations:
(Select ALL that are valid).
Answer: int a=1102;
long b=1;
Java:
String d="";
C#:
string d="";
◉ Which of the following blocks of code will successfully ask a user
for a number, multiply that number by 2, and print the result.
Answer: Java
import java.util.Scanner;
,System.out.println("Enter a number to be doubled:");
Scanner myscanner = new Scanner(System.in);
int num=myscanner.nextInt();
num*=2;
System.out.println("Your number doubled is "+num);
◉ Java
boolean a=false;
boolean b=true;boolean c=false;boolean d=true;
if((a) && (b || c) && d) {
System.out.println("Yes");
}else {
System.out.println("No");}.
Answer: No
◉ The following code will not compile as is. It is missing a line that
needs to be inserted where the "//Missing Line" comment is. Which
line of code will fix the compile error?
class House {
public int square_footage;
public int stories;
, public House() {
square_footage=0;stories=1;}}
class Main {public static void main(String[] args) {
House h1;
//Missing Line
h1.square_footage=1;}}.
Answer: h1 = new House();
◉ class Stuff {
public int number=1;
}
class Main {
public static void do_things(int y) {
y+=3;
}
public static void do_other_things(Stuff y) {
y.number+=3;
}
public static void main(String[] args) {
int a=3;
Stuff myStuff = new Stuff();
do_things(a);