Switch Statements – Exercises
Quiz Summary
0 of 17 Questions completed
Questions:
Information
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
Results
Results
0 of 17 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
Categories
- Not categorized 0%
-
Unfortunately, your results in the simulator were not up to the desired standards. 😔
However, take advantage of your unlimited access and continue to practice.
Remember, persistence and dedication will lead to mastery. 😎 -
Congratulations! 🎉
You have successfully passed the simulator and are now one step closer to achieving your certification.
We hope you will consider joining us again in your future certification journey.Wishing you the best of luck with the upcoming exam. Keep up the great work! 💪
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- Current
- Review
- Answered
- Correct
- Incorrect
-
Question 1 of 17
1. Question
You’re working on a system for an online store that processes orders. Each order has a status code (
orderStatus).- For status code 0, display ‘Order Received’
- For status code 1, display ‘Order Processed’
- For status code 2, display ‘Order Shipped’
- For status code 3, display ‘Order Delivered’
- For any other code, display ‘Invalid Status’
Using the provided framework, fill in the blanks:
-
Integer orderStatus;
// Your code here
switch on {
when 0 {
System.debug( );
}
when {
System.debug(‘Order Processed’);
}
when 2 {
System.debug( );
}
when {
System.debug(‘Order Delivered’);
}
when else {
System.debug( );
}
}
CorrectIncorrect -
Question 2 of 17
2. Question
Imagine you’re working on an application for a bookstore. The system should provide a message based on the genre code of a book (
genreCode):- For code 100, display ‘Fiction’
- For code 200, display ‘Non-Fiction’
- For code 300, display ‘Children’s Books’
- For any other code, display ‘Genre Not Recognized’
Complete the code:
-
Integer genreCode;
// Your code here
switch on {
when {
System.debug(‘Fiction’);
}
when {
System.debug(‘Non-Fiction’);
}
when {
System.debug(‘Children\’s Books’);
}
when else {
System.debug(‘Genre Not Recognized’);
}
}
CorrectIncorrect -
Question 3 of 17
3. Question
You are building a system for a local zoo. Depending on the type of ticket purchased (
ticketType), the system should print a message to the debug log:- For a ‘Child’ ticket, print ‘Child ticket – Free entry!’
- For an ‘Adult’ ticket, print ‘Adult ticket – $10!’
- For a ‘Senior’ ticket, print ‘Senior ticket – $5!’
- For any other type of ticket, print ‘Unknown ticket type!’
Fill in the blanks:
-
String ticketType;
// Your code here
switch on {
when {
System.debug(‘Child ticket – Free entry!’);
}
when {
System.debug(‘Adult ticket – $10!’);
}
when {
System.debug(‘Senior ticket – $5!’);
}
when else {
System.debug(‘Unknown ticket type!’);
}
}
CorrectIncorrect -
Question 4 of 17
4. Question
You’re creating an application that classifies users based on their roles. Given a string variable
role, fill in the blanks to classify and print messages for “Admin”, “User”, and any other role:-
String role = ‘User’;
switch on {
when ‘Admin’ {
System.debug(‘Hello, administrator!’);
}
when {
System.debug(‘Hello, regular user!’);
}
when else {
System.debug( + ‘ role is not recognized.’);
}
}
CorrectIncorrect -
-
Question 5 of 17
5. Question
Given a Salesforce Lead object, you want to convert leads based on their
LeadSource. If the source is'Web', you want to route them to a web lead processing method. If the source is'Phone', route to a phone lead processing method, and for all other sources, route to a default processing method.-
public void processLead(Lead ld) {
switch on {
when ‘Web’ {
webLeadConversion( );
}
when ‘Phone’ {
(ld);
}
when else {
defaultConversion( );
}
}
}
public void webLeadConversion(Lead l) {
// logic for Web leads
}
public void phoneLeadConversion(Lead l) {
// logic for Phone leads
}
public void defaultConversion(Lead l) {
// logic for other lead sources
}
CorrectIncorrect -
-
Question 6 of 17
6. Question
Imagine a library system where you are tracking the status of a book (
bookStatus):- For status code 0, display ‘Available’
- For status code 1, display ‘Borrowed’
- If the status is null, display ‘Status Unknown’
- For any other status, display ‘Invalid Status’
Implement the logic using the structure provided:
-
Integer bookStatus;
// Your code here
switch on {
when {
System.debug( );
}
when {
System.debug( );
}
when {
System.debug( );
}
when else {
System.debug( );
}
}
CorrectIncorrect -
Question 7 of 17
7. Question
You are building a program to determine the discount rates for products based on their categories. The categories are represented as strings: “Books”, “Electronics”, “Clothing”, and “Accessories”. Both “Books” and “Electronics” receive a 10% discount, “Clothing” receives a 15% discount, and any other category does not receive a discount. Fill in the blanks:
-
String productCategory = ‘Books’;
switch on {
when ‘Books’, {
System.debug(‘The discount is 10%.’);
}
when ‘Clothing’ {
System.debug( );
}
when else {
System.debug(‘No discount available.’);
}
}
CorrectIncorrect -
-
Question 8 of 17
8. Question
You are building a notification system for an online store. The notifications are based on order status. For statuses “Processing” and “Pending”, you notify the user that the order is still being processed. For “Shipped” and “Out for Delivery”, you notify that it’s en route. For “Delivered”, you confirm delivery.
Fill in the blanks:
-
String orderStatus = ‘Pending’;
switch on {
when ‘Processing’, {
System.debug(‘Your order is still being processed.’);
}
when ‘ ‘, ‘Out for Delivery’ {
System.debug(‘Your order is en route.’);
}
when ‘ ‘ {
System.debug(‘Your order has been delivered.’);
}
when else {
System.debug(‘Invalid order status.’);
}
}
CorrectIncorrect -
-
Question 9 of 17
9. Question
You’re coding an application to classify customer feedback based on ratings. The ratings are integers from 1 to 7. Ratings 1 to 3 are considered “Negative”, 4 to 6 are “Neutral”, and 7 is “Positive”.
Given an integer
rating, fill in the blanks to correctly classify and print the feedback type:-
Integer rating = 4;
switch on {
when 1, 2, 3 {
System.debug( );
}
when 4, 5, 6 {
System.debug(‘Feedback is Neutral.’);
}
when {
System.debug(‘Feedback is Positive.’);
}
when else {
System.debug(‘Invalid rating value.’);
}
}
CorrectIncorrect -
-
Question 10 of 17
10. Question
A car dealership in Salesforce manages records for Cars and Trucks. You’ve received an sObject variable named
vehicle, and you need to determine its type. If it’s a Car, you’ll debug ‘This is a car’, if it’s a Truck, you’ll debug ‘This is a truck’.Fill in the gaps:
-
if (vehicle Car) {
Car c = (Car) ;
System.debug(‘This is a ‘);
} else if (vehicle instanceof Truck) {
Truck t = (Truck) vehicle;
System.debug(‘This is a truck’);
} else {
System.debug(‘Not a recognized vehicle type’);
}
CorrectIncorrect -
-
Question 11 of 17
11. Question
A travel agency uses Salesforce to manage its Tour Packages and Hotel Bookings. You’re required to determine the type of sObject provided and then cast it to the correct type and log a respective message.
Fill in the blanks:
-
if (sobject instanceof TourPackage) {
tp = (TourPackage) sobject;
System.debug(‘This is a tour package: ‘ + tp);
} else if (sobject HotelBooking) {
HotelBooking hb = (HotelBooking) sobject;
System.debug(‘This is a hotel booking: ‘ + hb);
} else {
System.debug(‘Not a recognized booking type’);
}
CorrectIncorrect -
-
Question 12 of 17
12. Question
Your company maintains a database of Leads and Opportunities in Salesforce. You need to check the type of sObject provided and log a message accordingly. If it’s a Lead, you’ll log ‘This is a lead’, if it’s an Opportunity, you’ll log ‘This is an opportunity’, otherwise you’ll log ‘Unknown sObject type’. Fill in the blanks:
-
if (sobject instanceof ) {
Lead l = (Lead) sobject;
System.debug( );
} else if (sobject instanceof Opportunity) {
Opportunity o = (Opportunity) sobject;
System.debug(‘This is an opportunity’);
} else {
System.debug(‘Unknown sObject type’);
}
CorrectIncorrect -
-
Question 13 of 17
13. Question
You have an sObject variable named
businessRecord. If it’s anAccount, you should print the account’s industry; if it’s aContact, print the contact’s email; for any other type or null, print a default message.Fill in the blanks:
-
switch on {
when Account acc {
System.debug(‘Account Industry: ‘ + );
}
when con {
System.debug(‘Contact Email: ‘ + );
}
when null {
System.debug(‘No data available.’);
}
when else {
System.debug(‘Unsupported record type.’);
}
}
CorrectIncorrect -
-
Question 14 of 17
14. Question
A university manages records for Students and Professors. You’ve been given an sObject variable named
person. Identify its type and print the respective message.Complete the code:
-
switch on person {
when Student s {
System.debug(‘This is a ‘ + s);
}
when p {
System.debug(‘This is a professor ‘ + p);
}
when else {
System.debug(‘Person type not recognized’);
}
}
CorrectIncorrect -
-
Question 15 of 17
15. Question
You have an Enum named
Monthsrepresenting different months of the year. You want to determine the season based on the month. When it’sJUNE,JULY, orAUGUST, it’s summer. When it’sDECEMBER,JANUARY, orFEBRUARY, it’s winter.Fill in the blanks:
-
enum Months {JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER}
switch on {
when , , {
System.debug(‘winter’);
}
when JUNE, , {
System.debug(‘summer’);
}
when else {
System.debug(‘other seasons’);
}
}
CorrectIncorrect -
-
Question 16 of 17
16. Question
Imagine you have an Enum named
Daysrepresenting days of the week. You want to determine which days are weekend days. When it isSATURDAYorSUNDAY, it’s a weekend. Otherwise, it’s a weekday.Fill in the blanks:
-
switch on {
when SATURDAY, {
System.debug( );
}
when else {
System.debug(‘weekday’);
}
}
CorrectIncorrect -
-
Question 17 of 17
17. Question
You have an Enum named
Shapes. You want to decide the number of sides for a given shape. ATRIANGLEhas 3 sides, aSQUAREhas 4 sides. Any other shape has an undetermined number of sides.Fill in the blanks:
-
switch on {
when TRIANGLE {
System.debug( );
}
when {
System.debug(‘4 sides’);
}
when else {
System.debug(‘unknown number of sides’);
}
}
CorrectIncorrect -