Docsity

CSE 1322 EXAM 3 QUESTION SET WITH RATIONALES AND PROGRAMMING LOGIC AND JAVA IMPLEMENTATION, Exams of Java Programming

Toggle sidebar
bg1
CSE 1322 EXAM 3 QUESTION SET WITH
RATIONALES AND PROGRAMMING LOGIC
AND JAVA IMPLEMENTATION REVIEW 2026
◉ Which of the following blocks of code will successfully ask a user
for a number, multiply that number by 2, and print the result
A.Java
import java.util.Scanner;
System.out.println("Enter a number to be doubled:");
Scanner myscanner = new Scanner(System.in);
String num=myscanner.NextLine();
num*=2;
System.out.println("Your number doubled is "+num);
B.Java
import java.util.Scanner;
System.out.println("Enter a number to be doubled:");
Scanner myscanner = new Scanner(System.in);
int num=myscanner.nextInt();
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a

Partial preview of the text

Download CSE 1322 EXAM 3 QUESTION SET WITH RATIONALES AND PROGRAMMING LOGIC AND JAVA IMPLEMENTATION and more Exams Java Programming in PDF only on Docsity!

CSE 1322 EXAM 3 QUESTION SET WITH

RATIONALES AND PROGRAMMING LOGIC

AND JAVA IMPLEMENTATION REVIEW 2026

◉ Which of the following blocks of code will successfully ask a user for a number, multiply that number by 2, and print the result A.Java import java.util.Scanner; System.out.println("Enter a number to be doubled:"); Scanner myscanner = new Scanner(System.in); String num=myscanner.NextLine(); num*=2; System.out.println("Your number doubled is "+num); B.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); C.Java import java.util.Scanner; int num; System.out.println("Enter a number to be doubled:"); Scanner myscanner = new Scanner(System.in); myscanner.nextInt(num); num=2; System.out.println("Your number doubled is "+num);. 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); ◉ Arrays are not fixed length and can grow or shrink depending on if the size of the data set varies.. Answer: False

class Numbers { public int a; public int b; public Numbers(int c) { a=c; b=c*2; } @Override public String toString() { return("a: "+a+" b: "+b); }} class Main {public static void main(String[] args) { Numbers n1=new Numbers(2); System.out.println(n1); }} n No output or an error a: 2 b: 4 Numbers. Answer: a: 2 b: 4 ◉ How many parameters does a default constructor have? 3 2

Answer: 0 ◉ Which of the following methods are invalid? Java public static void do_stuff_1() {return;}public static int do_stuff_2() {return 2;}public static char do_stuff_3() {return 'c';}public static boolean do_stuff_4() {return false;} do_stuff_1() do_stuff_2() do_stuff_3() do_stuff_4() They are all valid. Answer: they are all valid ◉ The following code will compile and output 8? class Stuff {public int x=7;} class Main { public static void main(String[] args) { Stuff.x=8;

◉ class Main {public static int dostuff(int y) {return y+=5;} public static void main(String[] args) {int x=5;for(int i=0;i<2;i++) {x=dostuff(x);}System.out.println("x:"+x);}} x: x: x: x:10. Answer: x: ◉ import java.util.ArrayList; class Stuff {private int x; public void setx(int y) {x=y;} public int getx() {return x;}} class Main { public static void main(String[] args) { ArrayList myNumbers = new ArrayList(); for(int i=0;i<5;i++) { Stuff myStuff = new Stuff(); myStuff.setx(i*10); myNumbers.add(myStuff);

System.out.println(myNumbers.get(2).getx()); } } 0 2 10

Answer: 20 ◉ What is the output of the following code? What is the output of the following code?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; }

Hi from dostuff 2 Hi from dostuff 3 Hi from dostuff 4 Hi from dostuff 1 Hi from dostuff 2 Hi from dostuff 3 Hi from dostuff 2 Hi from dostuff 2 Hi from dostuff 2 This produces a compile error saying something like dostuff is already defined.. Answer: Hi from dostuff 2 Hi from dostuff 3 Hi from dostuff 4 ◉ 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]); } } 1 2 3

Answer: 1 ◉ import java.util.ArrayList; class Main { public static void main(String[] args) { ArrayList myNumbers = new ArrayList(); myNumbers.add(10);myNumbers.add(20);myNumbers.add(30);my Numbers.add(40); System.out.println(myNumbers.get(myNumbers.size()-1));}} 10 20

◉ Why does the following code not compile? Java public class XYZ { private int myNum; public XYZ() { myNum=0; } public XYZ(int n1) { myNum=n1; }} public class ABC extends XYZ { private int num2; public ABC() { myNum=0; num2=0; } pubilc ABC(int n1) { myNum=n1; num2=n1*2; }} class Main { public static void main(String[] args) { ABC myABC = new ABC(); }}

A.myNum is defined as private in XYZ and as such is inaccessable in ABC. B.class ABC cannot have private attributes because it's inheriting from XYZ C.class ABC is not allowed to inherit from class XYZ D.You cannot instantiate an object of type ABC, you can only instantiate objects of type XYZ. Answer: A.myNum is defined as private in XYZ and as such is inaccessable in ABC. ◉ To facilitate encapsulation, most attributes and methods in a class should be private? True False. Answer: True ◉ Given the following UML, which statement below is not valid? A myvar = new A(); B myvar = new B(); C myvar = new C(); A myvar = new B();

◉ What is the output of the following code: class Animal { public void sound() { System.out.println("eek"); }} class Cow extends Animal { @Override public void sound() { System.out.println("moo"); }} class Dog extends Animal {} class Main { public static void main(String[] args) { Animal a1=new Animal(); a1.sound(); Cow a2=new Cow(); a2.sound(); Dog a3=new Dog(); a3.sound(); }} a3.sound() will produce a compile error. eek moo woof moo

eek moo eek moo eek. Answer: eek moo eek ◉ Will the following code compile? interface IA {public void do_stuff1();public void do_stuff2();} class C implements IA { @Override public int do_stuff1() { return 1; } @Override public void do_stuff2() { int b=1; } }

public abstract int do_stuff1(); public abstract int do_stuff2(); } class B extends A { public B() { super(7); } public B(int y) { super(y); } @Override public int do_stuff1() { return 4; } @Override public int do_stuff2() { return 4; } } class Main { public static void main(String[] args) { B myB = new B(5);

No, class A cannot have a constructor since it's abstract. No, class B can't have a constructor since it's inheriting from an abstract class A. No, you can't use super/base to call a constructor in an abstract class. Yes. Answer: Yes ◉ The following is a valid (i.e. will compile) abstract class definition: abstract class A { public int do_stuff1() { return 3; } public int do_stuff2() { return 4; } } True False. Answer: true