CSE 174 Exam 3
whats higher in precedence &&/|| or !?
!
How do you get the last digit of a number?
%10
write and expression that returns a random integer between 50 and 99, bot inclusive
(int) (Math.random () *50) + 50
the file produced by the java compiler has what extension?
.class
what method should you use to compare strings ?
.equals()
int x=1; (x>==1) || (x++>1) whats the value of x after the evaluation of the expression
1 because if the 1st one is true it doesn't even read the second part
How to use Math.random() to produce from 1 to 9?
1+(int) (Math.random()*9)
what is the value after the code is executed? int num=5, ans=1; num=ans++; num=? ans=?
1,2
int num1= 10; if(num1==10){ int num2=11; system.out.println(num1+" " +num2); } int num2=12; System.out.println(num1+ " " + num2);
10,11 10,12
int 101-1/10=?
101
what is the size of a char data type in java
16 bits
what is the output? for x=2 System.out.print("A"); if(x==2){ System.out.print("B"); } if(x==3){ System.out.print("C"); } else{ System.out.print("D"); } System.out.print("E);
ABDE
which of the following is a valid initialization of a Boolean variable? A) boolean flag=0; B) boolean flag=true; C)boolean flag=no; D) boolean flag= "true";
B
what does the following code snippet print? int a=10, b=20, c; c=b/a if(c>0){ int ab; ab =a*b+c; } System.out.print(ab);
Error: cant find symbol; variable ab
what is the output of the code boolean isStudying = false; if("true") is studying=true;
Error: incompatible types
What is a runtime error also known as?
Exception
T or F in java 1/3+1/3+1/3 = (1+1+1)/3
False
what is the boolean expression in java of (0.1+0.1+0.1==0.3)
False
java's predefined classes are grouped into what?
Packages
what does %s do in print f
String Capital S will uppercase all the letters in a string
What is variable?
a named storage location in the computers memory. ( ie System.out.print(value);)
What is literal?
a value that is written into the code of a program
int x=11; int y=14; if(x>10){ System.out.println("a"); if(x>10){ System.out.println("b"); } } else if(y>10){ if(x==1) System.out.println("c"); }
a,b
double x=2.0; if(x==2.0) System.out.println("a"); if(x==3.0) System.out.println("b"); else System.out.println("c");
a,c
what data type is returned by equals() method of the String class?
boolean
what do you need in a switch statement in order to stop it from going through all the lines of code?
break;
What are the 8 primitive data types?
byte short int long float double boolean char
what does \" do?
causes a double quotation mark to be printed
what does \' do?
causes a single quotation mark to be printed
what does \\ do ?
causes one backslash to be printed
what does compareTo() method do?
compare lexicology of the words
what does equalsIgnoreCase() do?
compares the string ignoring the case of it
What is Logic Errors?
compilers and runs, but with unexpected results
what is Runtime Errors?
compiles, but then crash/ not discovered by the compiler
what does %d do in printf
decimal integer
what does declaring a variable do?
declaring gives the data type and name of the variable
what does void in a method do?
doesnt return anything, usually prints something
What is the order of java program development ?
editor to source code(.java) to java compiler to Byte code(.class) to JVM to program execution
what is compiler Errors?
error that prevents the program from compiling
int num=10; if(num%2==0){ String result="even"; } else{ String result="odd"; } System.out.println(result);
error, because result wasn't declared before the if statement
what is the default initialization of a boolean variable?
false
what does %f do in printf
floating-point number
Byte Code
generated by the compiler(.class file)
what does str.charAt() do?
gives the character at the position inputted
what does str.length() do?
gives the length of the string
what doe substring() do?
gives the substring of the specific word. From the first number to the position before the last number
compiler
identifies syntax Errors and a program that converts a program in one language to another language
How do you import the Scanner class in Java?
import java.util.Scanner;
Java Virtual Machine
interprets the Byte code
What is an IDE ?
it combines an editor, compiler, and interpreter into one package
what does initializing a variable do ?
it gives the variable a value
what is narrow casting ?
its casting that requires explicit type casting because the target type is smaller than the source type
identifiers may only contain
letters a-z or A-Z, the digits 0-9, underscores(_) or $, the first character cant be a digit, no space, and are case sensitive
Are all java reserved words in uppercase or lowercase
lowercase
what does %n do in printf
newline
void test1 (int x){ System.out.println(x); } wil int a=test1(2); work? if so whats the answer?
no because a cant equal a print statement
double test3 (double y){ return y+2; } will int d= test3(4.5); work?
no because d is an int and it returns a double
String test2(int x){ return "Java " + x; } will String s= test2(10.0) work?
no because you cant have a double when the parameters are int
void test1 (int x){ System.out.println(x); } wil System.out.println(test1(3)); work? if so whats the answer?
no because you cant print a print
String test2(int x){ return "Java " + x; } will int c = test2(40); work?
no, you cant have something equal to a word and number
how would you get 'g' from the following definition? String str ="strings";
str.charAt(str.length()-2);
what does str.indexOf() return ?
the 1st index of the input inside of (). If it cant find it it returns -1
what is Scope?
the parts of the program where the variable is visible
what does a method use to call the value back to the calling method
the return statement
what is the output of the code int num=0; System.out.println(num>=0 || 10/num>=0);
true
int x=1; System.out.println((x!=1) == !(x==1)); System.out.println((X>0) || (x<0)); System.out.println((x>0) && (x<0));
true,true,false
what does the Datatype.parseDatatype() do?
turns the inputted string in () to whatever data type is being used
block level variables
variables declared inside a block
local variables
variables declared inside a method
what is the return type of this method? public static void main (String[] args);
void
what is lifetime?
when a variable is born and when it dies
what is widening casting?
widening conversions are automatic type casting
double test4( int x, int y){ return x+y; } will System.out.print(test4(5,6)); work?
yes, 11.0
double test4( int x, int y){ return x+y; } will int g= (int)test4(1,2); work?
yes, 3
double test3 (double y){ return y+2; } will double e = test3(5); work?
yes, 7.0
double test4( int x, int y){ return x+y; } will double g= test4(3,4); work?
yes, 7.0
double test3 (double y){ return y+2; } will double f = test3(6.5);; work?
yes, 8.5
void test1 (int x){ System.out.println(x); } wil test(1); work? if so whats the answer?
yes,1
Applets
small applications that require the use of a java enabled web browser
what would a.compareTo(b) be?
<0 because a is before b
int x=1; (x>==1) && (x++>1) whats the value of x after the evaluation of the expression
2
what is the output? a=10 b=2 c=3 d=4 e=5 a=a>b?c>d?1:e<d?2:3:4;
2
what value of c belongs in the blank line int x=10; int c= ___; // line 2 switch(c){ case1: x+=2; case2: x+=2; defult: x+=1; } System.out.println(x); //prints 13
2
what are the values printed after the snippet is executed? int val1=2; double val2=++vall * 10 +2.0; val1=? val2=?
3,32.0
what is the value of '1'?
49
what does %c do in printf
Character Capital C will uppercase the letter
What is the interpreter in java and what does it do?
JVM it converts one line of code from one language to another than executes the the instructions
String test2(int x){ return "Java " + x; } will System.out.println(test2(10)); work?
Java 10
write an expression to check if the double values a and b are very close together
Math.abs(a-b)<.0001
what Math method will produce an answer that is closest in value to a double d, while not being greater than d in int floor
Math.floor
what does contains() do?
sees if a string contains the other string in it
an if statement must have a boolean expression and that boolean expression must be enclosed in parentheses ?
True
What are the rules for naming a class?
Uppercase letter to start it, no spaces
how do you create a method?
public static (return type) (method name)(argument type & argument variable){ }
How do you create a Constant?
put final at the beginning then the data type then uppercase name separated by an underscore
write a method that returns 0 if the boolean argument boobs is false, 1 otherwise
return (boobs==true)?1:0;
write a return method that returns the int sum of the arguments int num1 and double num2
return (int) (num1+num2);
write a return method that returns the length of the longest string of S1 and S2
return s1.length()>s2.length?s1.length():s2.length();
write a method that returns only the first letters of the two string arguments str1 and str2
return str1.substring(0,1)+str2.substring(0,1);
write a method that returns if at least one of the arguments num1 and num2 is greater than but not equal to 50 and the sum of the two numbers is even and returns false otherwise
return(num1>50 ||num2>50) && ( (num1+num2) %2==0);
