QUESTION 32 Assume three String variables named student1, student2, and student3
ID: 3740540 • Letter: Q
Question
QUESTION 32
Assume three String variables named student1, student2, and student3 that have been initialized with students’ last names. A Stringvariable first has also been declared.
Complete the code fragment that will result in first containing the student last name that is first alphabetically.
first = student1;
if(student2.compareTo(first) > 0)
{
}
if(student3.compareTo( > 0)
{
}
10 points
QUESTION 33
Assume that symbol is a char variable has been declared and already given a value. Write an expression whose value is true if and only if symbol is alphanumeric, that is either a letter or a decimal digit.
(symbol choose answer<<===>>=&&|| 'A' choose answer<<===>>=&&|| symbol choose answer<<===>>=&&|| 'Z') choose answer<<===>>=&&|| (symbol choose answer<<===>>=&&|| 'a' choose answer<<===>>=&&|| symbol choose answer<<===>>=&&|| 'z') || (symbol choose answer<<===>>=&&||'0' choose answer<<===>>=&&|| symbol choose answer<<===>>=&&|| '9')
10 points
QUESTION 34
Which expression(s) are an exact match to the regular expression p*pkk*kk*?
Choose Yes or No for each string of characters
ppp
choose answeryesno
kkkkk
choose answeryesno
pk
choose answeryesno
pkk
choose answeryesno
ppkk
choose answeryesno
ppp
choose answeryesno
kkkkk
choose answeryesno
pk
choose answeryesno
pkk
choose answeryesno
ppkk
choose answeryesno
Explanation / Answer
Q 32 - compareTo gives >0 if argument is smaller than the callee. In case of strings it look lexicographically.
So solution is
first = student1;
if(student2.compareTo(first) > 0)
{
// Student2 now has the smaller string(alphabetically) as student2 was > student1 so set student2 = student1
student2 = student1;
}
// If it hadn't gone in the above if clause means that student2 was already smaller than student1
// Compare with student2 as now student2 has smaller of student1 and student2
if(student3.compareTo(student2) > 0)
{
// Same as above make student3 the minimum of student3 and student2
student3 = student2;
}
// As now student3 has the minimum
first = student3;
Another way will be to use if else clause and that would be easy but I thought that question required not to introduce and more if else clauses.
Q 33
Use java inbuilt function. For c/c++ compare with ASCII table
{
return True;
}
Q 34
PPP - NO, As 2nd p, 1st k and 3rd K of p*pkk*kk* is necessary to come in the string
kkkkk - NO, Same as above
pk - NO, Same as above
pkk - YES, You can select 0 or more if * is there. So this can be generated by taking 0 of all *
ppkk - YES, Take 1 from 1st * and 0 from other * and compulsory pkk. It becomes ppkk
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.